“I bought this guide a few days ago to prepare for my interview with Oracle. Many of the questions they asked me were from this guide. I found this book absolutely great!”
to call virtual contructor, the
V-table should be already in memory , however the there is no pointer to v-table in memory because the object has not been created ..and the object will be created when you have the cionstructor in place…kind of a viscious circle
Vtable is created when a class contains atleast one virtual function.So, vtable is created per class. All the virtual function is stored there. To access those virtual function , each object has vptr(virtual pointer). So vptr is created per object. vptr is initialized by constructor of that class. But when the constructor is created virtual then how the vptr is intialized. but destructor can be made virtual.
Constructor cant be virtual because it needs virtual table n that will be vreated only when object is created. and object is created only when he constructor is called.
Distructor can be made virtual because when we pass the class object as the parameter to the function in the class, it creats the copy of that class. whenevr the object is created in its derived class and it goes out of scope…the copy destructor will be called instead of normal destructor.
Vitual destructors exist so that when a derived object is destroyed the compiler knows what object in the inheritance chain is responsible for controlling the destruction.
Virtual constructors are not needed because the object you are instatiating should always be in control of the construction process if it chooses so. If not it can delegate construction to the appropriate base class.
Submitted By: pola.peda venkateswarlu — October 11, 2007
First let us discuss about the concept of VIRTUAL.
We wish to maintain any thing as VIRTUAL to be overridden by the derived classes things.So to invoke override things , we need an instance. To create a derived class instance, base class constructor must be invoked due to inheritance. Hence VIRTUAL CONSTRUCTOR is meaning less.
That’s why..To invoke derived DESTRUCTOR, we should make base DESTRUCTOR as VIRTUAL.
I worked in Delphi for many years and often used virtual constructors with no problem. The C++ just do not support it because of standard, possibly based on specific implementations and such changes will make old code incompatible.
The lack of virtual constructors has nothing to do with the v-table, which is in fact, in place before the constructor is invoked. There is simply no need for virtual constructors as the constructor is selected by the instantiating code or derived class.
A virtual destructor allows derived classes to cleanup if deleted through a base class pointer.
Conceptually there is no need for the virtual constructor. Virtual function call mechanism is created to be able to invoke different version of a function depending on the objects type that pointer points to.
But when the constructor is called, you are always aware of the type of the object you’re creating. You are never creating an object via pointer or anything for that matter. On the stack or on the heap, the object is created by clearly specifying its type. There’s no need for virtual mechanism.
You do need virtual destructor though, to make sure the destructor of the derived method is called when the object is deleted using the pointer to its parent type. Otherwise memory leaks would occur.
First: A few comments: first vptr is implementation dependent. I think it should not be part of the answer.
Second, We can initialize the object and then the table or visa versa…I do not see the problem here…
Here is my thought why there is no virtual constructor: virtual means: during the run time look at the type of the object when u invoke this function. In the case of constructor, we know the type of the object…we are creating it now…”the type is on front of our eyes”.
Actually virtual constructors are very nifty for another thing which C++ doesn’t have, metaclasses (the name has many meanings so let me explain the one I am hinting at).
In Object Pascal (Delphi, FPC), you can make a base class with virtual constructor. Then you create a “metaclass” out of this class which can be put into a variable like:
TFirst = class
end;
TFirstClass = class of TFirst;
var
x: TFirstClass;
a: TFirst;
begin
x := TFourth; // set the metaclass to TFourth
a := x.Create;
{ Since constructor is virtual, it will create a
} TFourth, even if x is “class of TFirst”
end.
Think of it as runtime templates for objects, quite nifty in some situations when you need to switch types of objects you create (classfactory).
constructors are always created top to down order, where as destructor’s are called bottom up order. this is fine until the following case.
Baseclass *bp= new Derivedclass;
….. do some operatons….
delete bp;
here the bp is pointing to derived pointer, but we are deleting bp; it will call base destructor rather than derived destructor which is only possible if we create base destructor as virtual.
to call virtual contructor, the
V-table should be already in memory , however the there is no pointer to v-table in memory because the object has not been created ..and the object will be created when you have the cionstructor in place…kind of a viscious circle
Vtable is created when a class contains atleast one virtual function.So, vtable is created per class. All the virtual function is stored there. To access those virtual function , each object has vptr(virtual pointer). So vptr is created per object. vptr is initialized by constructor of that class. But when the constructor is created virtual then how the vptr is intialized. but destructor can be made virtual.
Constructor cant be virtual because it needs virtual table n that will be vreated only when object is created. and object is created only when he constructor is called.
Distructor can be made virtual because when we pass the class object as the parameter to the function in the class, it creats the copy of that class. whenevr the object is created in its derived class and it goes out of scope…the copy destructor will be called instead of normal destructor.
Vitual destructors exist so that when a derived object is destroyed the compiler knows what object in the inheritance chain is responsible for controlling the destruction.
Virtual constructors are not needed because the object you are instatiating should always be in control of the construction process if it chooses so. If not it can delegate construction to the appropriate base class.
First let us discuss about the concept of VIRTUAL.
We wish to maintain any thing as VIRTUAL to be overridden by the derived classes things.So to invoke override things , we need an instance. To create a derived class instance, base class constructor must be invoked due to inheritance. Hence VIRTUAL CONSTRUCTOR is meaning less.
That’s why..To invoke derived DESTRUCTOR, we should make base DESTRUCTOR as VIRTUAL.
I worked in Delphi for many years and often used virtual constructors with no problem. The C++ just do not support it because of standard, possibly based on specific implementations and such changes will make old code incompatible.
The lack of virtual constructors has nothing to do with the v-table, which is in fact, in place before the constructor is invoked. There is simply no need for virtual constructors as the constructor is selected by the instantiating code or derived class.
A virtual destructor allows derived classes to cleanup if deleted through a base class pointer.
How about this one:
Conceptually there is no need for the virtual constructor. Virtual function call mechanism is created to be able to invoke different version of a function depending on the objects type that pointer points to.
But when the constructor is called, you are always aware of the type of the object you’re creating. You are never creating an object via pointer or anything for that matter. On the stack or on the heap, the object is created by clearly specifying its type. There’s no need for virtual mechanism.
You do need virtual destructor though, to make sure the destructor of the derived method is called when the object is deleted using the pointer to its parent type. Otherwise memory leaks would occur.
First: A few comments: first vptr is implementation dependent. I think it should not be part of the answer.
Second, We can initialize the object and then the table or visa versa…I do not see the problem here…
Here is my thought why there is no virtual constructor: virtual means: during the run time look at the type of the object when u invoke this function. In the case of constructor, we know the type of the object…we are creating it now…”the type is on front of our eyes”.
Actually virtual constructors are very nifty for another thing which C++ doesn’t have, metaclasses (the name has many meanings so let me explain the one I am hinting at).
In Object Pascal (Delphi, FPC), you can make a base class with virtual constructor. Then you create a “metaclass” out of this class which can be put into a variable like:
TFirst = class
end;
TFirstClass = class of TFirst;
TSecond = class(TFirst);
TThird = class(TSecond);
TFourth = class(TThird);
var
x: TFirstClass;
a: TFirst;
begin
x := TFourth; // set the metaclass to TFourth
a := x.Create;
{ Since constructor is virtual, it will create a
} TFourth, even if x is “class of TFirst”
end.
Think of it as runtime templates for objects, quite nifty in some situations when you need to switch types of objects you create (classfactory).
constructors are always created top to down order, where as destructor’s are called bottom up order. this is fine until the following case.
Baseclass *bp= new Derivedclass;
….. do some operatons….
delete bp;
here the bp is pointing to derived pointer, but we are deleting bp; it will call base destructor rather than derived destructor which is only possible if we create base destructor as virtual.
Leave an Answer/Comment