0

I'd like to do the following:

void do_stuff(Base* base_ptr) {
   // here I need the overridden methods
   base_ptr->init();
}

class Base {
   Base() {
     do_stuff(this);
   }
   virtual void init() {}
};

class Derived : public Base {
   virtual void init() override {
      // Derived specific init
   }
}

But all I get are calls to Base::init(), is it even possible to do what I intend?

Teris
  • 600
  • 1
  • 7
  • 24

1 Answers1

2

You are calling a virtual function from within the constructor!

Duplicated of -> Calling virtual functions inside constructors

Community
  • 1
  • 1
simpel01
  • 1,792
  • 12
  • 13
  • Thanks, is it ok to "register" the object in some global container in the base constructor? The access to the virtual methods would happen at a later point. – Teris Nov 22 '15 at 12:01