Statically Init a derived class

Posted by AC on Stack Overflow See other posts from Stack Overflow or by AC
Published on 2010-05-15T12:25:44Z Indexed on 2010/05/15 12:34 UTC
Read the original article Hit count: 161

Filed under:

With c++, Is there a way to get a derived class to inherit its own static initializer? I am trying to do something like the following:

class Base {

public:
        class StaticInit {
                public:
                virtual StaticInit() =0;
        };
};



class Derived: public Base {
  public:
        virtual StaticInit::StaticInit() {
           //do something with the derived class
        }
        static StaticInit init;
  }

static Derived::StaticInit init;

it would also be nice if I didn't have to define the init var in each derived class. I am currently redefining the StaticInit internal class in each derived class but it seems redundant.

Each derived class is a singleton, and I need the instance to be stored in a lookup table at program startup.

© Stack Overflow or respective owner

Related posts about c++