enumcount

来自「ace开发环境 用来开发网络程序 其运用了设计模式、多平台、C++等多种知识」· 代码 · 共 96 行

TXT
96
字号
From: Gianni Mariani <gi2nospam@mariani.ws>Date: 26 Jul 2003 04:52:43 GMTNewsgroups: comp.lang.c++Subject: Re: enum countClive wrote:> If you have an enum, is there any way during execution to find the number of> values in the enum?> Say I have,> > enum great { five, ten, fifteen };> > How could I get the number 3 from that?> > replace the enums with objects that report themselves to a registry.I have done it in the past using a template ...templace <typename base>class ExposedEnum : public base{	public:	int	enum_val;		ExposedEnum( int number )	  : enum_val( number )	{		ExposedEnumRegister<base>::Register( *this );	}	ExposedEnum( int number )	  : enum_val( ExposedEnumRegister<base>::GetNextNumber() )	{		ExposedEnumRegister<base>::Register( *this );	}// some more stuff ...	operator int () const	{		return enum_val;	}	explicit ExposedEnum( const ExposedEnum & foo );};template <typename base>class ExposedEnumRegister{	static int GetNextNumber ....	static void Register ....	static int Count ....}Now you can forward declare them...extern ExposedEnum< great > five;extern ExposedEnum< great > ten;extern ExposedEnum< great > fifteen;In a cpp file you can instantiate them.ExposedEnum< great > five( 5 );ExposedEnum< great > ten( 10 );ExposedEnum< great > fifteen;Now, if you want to know how many you have :ExposedEnumRegister< great >::Count();Disclaimer - it's an outline only, yes it's incomplete.G$Id: EnumCount 66067 2005-05-24 04:33:13Z turkaye $

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?