Thursday, January 31, 2008

virtual constructor?

No.

When an object containing virtual functions is created, its VPTR must be initialized to point to the proper VTABLE. This must be done before there’s any possibility of calling a virtual function. Because the constructor has the job of bringing an object into existence, it is also the constructor’s job to set up the VPTR. The compiler secretly inserts code into the beginning of the constructor that initializes the VPTR.

call a virtual function inside a constructor?

only the local version of the function is used. Reason: Inside any constructor, you can only know that the base-class objects have been initialized, but you cannot know which classes are inherited from you. A virtual function call, however, calls a
function in a derived class. :When a constructor is
called, one of the first things it does is initialize its VPTR. However, it can only know that it is of the “current” type – the type the constructor was written for. When the compiler generates code for that constructor, it generates code for a constructor of that class, not a base class and not a class derived from it. -- thinkinginc++

No comments:

Post a Comment