Qualcomm Interview Question
Software Engineer / DevelopersWTF.
you can call virtual methods from derived class constructor. you cannot call virtual methods from base class constructor because, when you are creating the derived class object, first base class constructor is invoked and the base class object type is created. at this time, your derived class object is not yet created completely(only base class object part is created). so calling virtual method can be a problem. thats the reason c++ language doesn't allow you to call virtual methods in base class constructor.
You can perfectly call virtual methods in derived class constructor becuase the object is created.
You can call a virtual method from a derived class constructor but it would loose its meaning because it will call its own base implementation. This happens because "this" pointer within base constructor(called automatically when derived constructor is called) is of base type and therefore it will call its own implementation and the expected would be of derived.
http://www.parashift.com/c++-faq-lite/strange-inheritance.html#faq-23.5
- Veena September 04, 2009