简体   繁体   English

如何多态初始化`static`成员

[英]How to initialize `static` member polymorphically

Say I have base class Validator . 说我有基类Validator

class Validator
{
public:
  void validate(const string& str)
  {
    if( k_valid_keys.find(str) == k_valid_keys.end() ) 
      throw exception();
  }
private:
  const static std::set<string> k_valid_keys;
};

Now assume I need to extend class Validator . 现在假设我需要扩展Validator类。 Each derived class will have its own set of valid keys. 每个派生类将具有其自己的有效密钥集。
My goal is: 我的目标是:

  1. keep k_valid_keys a member of Validator . 保持k_valid_keysValidator的成员。 No need to add it to each derived classes especially when there are more than a few types of those. 无需将其添加到每个派生类中,尤其是当它们的类型不止一种时。
  2. keep k_valid_keys static . 保持k_valid_keys static Assume I have multiple instances of Validator (and its derived classed) and initialization of k_valid_keys is expensive. 假设我有多个Validator实例(及其派生类),并且k_valid_keys初始化非常昂贵。

How can I initialize static member polymorphically? 如何多态初始化static成员? well, I know that it can't be done (please correct if I'm wrong). 好吧,我知道这是不可能完成的(如果我错了,请更正)。

So Assuming it can't be done, any idea of a better design for this problem? 因此,假设无法解决这个问题,那么有没有一个更好的设计解决方案?

Since k_valid_keys is static declared at Validator , all the derived classes from Validator will share the same instance of k_valid_keys . 由于k_valid_keysstatic ,在申报Validator ,从所有的派生类Validator将共享相同的实例k_valid_keys That's it, you will not be able to have more than one instance of a subclass of Validator at the same time in your program, else your different instances of subclases of Validator will add elements to the same structure. 就是这样,你就不能有子类的多个实例Validator在你的程序的同时,否则你不同的subclases的实例Validator将元素添加到相同的结构。

That's is, a static member variable on a class is just a global variable of your entire program. 就是说,类上的静态成员变量只是整个程序的全局变量。

If you have two or more subclases of Validator but you guarantee that you are going to have only just one instance, you just initialize k_valid_keys on the subclass constructor. 如果您有两个或多个Validator但可以保证只有一个实例,则只需在子类构造函数上初始化k_valid_keys

You can do the following: instead of one set of the keys as static member, use a map mapping std::type_index to set of keys (or, if you cannot use C++11, std::type_info*). 您可以执行以下操作:使用映射映射std :: type_index代替一组键作为静态成员(或者,如果不能使用C ++ 11,请使用std :: type_info *)。 In each of your derived classes have a virtual function which returns the set of valid_keys for a class, and the validate function in the base class should get with typeid the type_info of the actual object, check if it already has the keys in the map - if not, the virtual function return the set is called, and the entry is added to the map. 在每个派生类中,都有一个虚函数,该虚函数返回一个类的valid_keys集,基类中的validate函数应使用typeid获得实际对象的type_info ,检查其在映射中是否已有键-如果不是,则调用返回该集合的虚函数,并将该条目添加到映射中。

Pass a reference to k_valid_keys in the constructor of your Validator classes and initialize a member reference. Validator类的构造函数中传递对k_valid_keys的引用,并初始化成员引用。 You'll skip construction for multiple Validator classes but you'll have to manage its lifetime. 您将跳过多个Validator类的构造,但必须管理其生命周期。

You can then have one per inherited type if you want or not as well. 然后,也可以根据需要为每个继承的类型设置一个。

Using a register of k_valid_keys that is accessed by type or key could also work. 使用按类型或键访问的k_valid_keys寄存器也可以工作。 Of course you would need to pass a reference to this register to your Validator classes. 当然,您需要将对该寄存器的引用传递给Validator类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM