简单地说,一个enum
仅仅是一个命名为恒定值,例如:
enum Settings
{
setting_number_0,
setting_number_1,
setting_number_2,
};
在上述例子中,setting_number_X
是简单地为值X
命名常量,如枚举值从0开始并增加单调。
保留这些,然后在某些类型的容器中给出了一个基本的整数存储类型,但仍然可能有点类型安全。
std::vector<Setting> app_settings;
// this works
app_settings.push_back(setting_number_0);
// this is a compile time failure, even though the underlying storage
// type for Setting is an integral value. This keeps you from adding
// invalid settings types to your container (like 13 here)
app_settings.push_back(13);
// but you also cannot (directly) add valid setting values (like 1)
// as an integral, this is also a compile time failure.
app_settings.push_back(1);
现在,假设您想添加其他特定设置类型并将它们全部保存在容器中。现在
enum DisplaySettings
{
// ...
};
enum EngineSettings
{
// ...
};
,如果你想保留的所有设置在一个容器中,你不能安全。您可以将所有积分值存储在容器std::vector<int>
或类似的容器中,但这样做会导致无法确定哪些整型属于设置枚举的内容。此外,由于类型不同,因此不能将它们存储在单个类型安全的容器中。
正确的方法去了解这是将存储设置的功能在容器中,这样的事情:
#include <vector>
#include <iostream>
// This is our "base class" type so we can store lots of
// different setting types in our container
class setting_action
{
public:
// we enable the setting by calling our function
void enable_setting()
{
setting_function_(this);
}
protected:
// This is a function pointer, and we're using it to get some
// compile time polymorphism
typedef void (*setting_function_type)(setting_action* setting);
// these can only be constructed by derived types, and the derived
// type will provide the polymorhpic behavior by means of the
// above function pointer and based on the derived type's handler
setting_action(setting_function_type func)
: setting_function_(func)
{
}
public:
~setting_action()
{
}
private:
setting_function_type setting_function_;
};
// This is the derived type, and where most of the magic
// happens. This is templated on our actual setting type
// that we define below
template <class Setting>
class templated_setting_action
: public setting_action
{
public:
templated_setting_action(Setting setting)
: setting_action(&templated_setting_action::enable_setting)
, setting_(setting)
{
}
// This function catches the "enable_setting" call from
// our base class, and directs it to the handler functor
// object that we've defined
static void enable_setting(setting_action* base)
{
templated_setting_action<Setting>* local_this =
static_cast<templated_setting_action<Setting>*>(base);
local_this->setting_();
}
private:
Setting setting_;
};
// this is just a shorthand way of creating the specialized types
template <class T>
setting_action* create_specialized_setting_action(T type)
{
return
new templated_setting_action<T>(type);
}
// Our actual settings:
// this one displays the user name
struct display_user_name
{
void operator()()
{
std::cout << "Chad.\n";
}
};
// this one displays a short welcome message
struct display_welcome_message
{
void operator()()
{
std::cout << "Ahh, the magic of templates. Welcome!\n";
}
};
// now, we can have one container for ALL our application settings
std::vector<setting_action*> app_settings;
int main()
{
// now we can add our settings to the container...
app_settings.push_back(create_specialized_setting_action(display_user_name()));
app_settings.push_back(create_specialized_setting_action(display_welcome_message()));
// and individually enable them
app_settings[0]->enable_setting();
app_settings[1]->enable_setting();
// also, need to delete each setting to avoid leaking the memory
// left as an exercise for the reader :)
return 0;
}
我认为这是从引用的文字多晶型'enums'别清晰至少从最简单的意义上说,这是一种工作。一个'enum'基本上是一个具有基础积分存储类型的命名常量。在任何你想使用polymorhpic'enum'的地方,使用模板可能会有更优雅的解决方案。 – Chad 2012-03-30 14:46:52
多态枚举没有多大意义,如果你认为你需要它们,那么你的设计很可能是以次充好的。如果你真的需要它们,那么Boost.Variant应该可以工作。 – Pubby 2012-03-30 14:49:52
@Chad你能帮助我更好地理解一些代码片段的例子吗? – Matteo 2012-03-30 14:55:29