Sunday, June 10, 2007

item 9

ver call virtual functions during construction or destruction

during base class construction virtual functions never go down into derived classes.

at what exact time does the derived class object come into existence ? at the momwnt the curly brace of constructor is entered ? or the moment the ) of arguments is seen ?

and similarly at what point is the derived object destructed ?

If you find that you need a virtual function call in the base class than you should have the dervied class pass the necessary info down to the base class and the non virtual function in base class can do the handling for you.

But make sure that thru no path of invocation, a virtual function is getting invoked till the constructor finishes ( or till some other point)? ( think insidiously...constructor calls non virtual function which calls a virtual function) need to make sure that this doesnt happen.

i think this is a big dilemma....if constructor cant call a virtual function then how do you provide extensiblity in the base class for things that need to be done during construction itself ?

things to remember:
dont call virtual functions during construction or destruction. because such calls will never go to a more dervied class than that of the currently executing constructor or destructor.

No comments: