static abstract methods in c++

前端 未结 3 2007
旧时难觅i
旧时难觅i 2020-12-06 04:20

I have an abstract base class

class IThingy
{
  virtual void method1() = 0;
  virtual void method2() = 0;
};

I want to say - \"all classes

3条回答
  •  悲哀的现实
    2020-12-06 05:03

    Static methods cannot be made virtual (or abstract, for that matter) in C++.

    To do what you're intending, you can have have an IThingy::factory method that returns a concrete instance, but you need to somehow provide a means for factory to create the instance. For instance, define a method signature like IThing* (thingy_constructor*)() and have a static call in IThingy that you can pass such a function to that defines how IThingy will construct the factory instance. Then, in a dependent library or class, you can call this method with an appropriate function that, in turn, nows how to properly construct an object implementing your interface.

    Supposing you haven't had your factory 'initializer' called, you'd want to take appropriate action, such as throwing an exception.

提交回复
热议问题