“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.
I read all the comments given here. Here is my opinion.
virtual is used to achieve the dynamic polymorphism concept in inheritance hierarchy. By this keyword we are making sure that the latest copy of that function ( most Derived function ) should be called which already explained in the the previous discussion using Vtable concept. Coming back to the original point about virtual constructor.
1. Kindly note that virtual keyword only used with function declaration not with definition.
2. There is nothing like Constructor declaration. There is always a constructor definition used to initialize the variable ( general purpose ).
3. The address mapping for virtual function is done at runtime on the base of type of object only, which is actually created by constructor. Because virtual keyword is in declaration to tell compiler to keep latest address of the virtual function where the definition of function is store in compile form.
4. Again in inheritance hierarchy. to deal with the most derived object, all the data member of hierarchy level must be initialize (irrespective weather object is created dynamically). Again we know that class having incomplete definition can not create an instance). So in constructor definition of most derived class we also have to supply values to initialize base class data member so that base constructor also will be called.
5. in case of virtual destructor, The scene is quite different. Here by giving virtual keyword to the destructor you are calling destructor of most recent object. ( as I told you earlier in meaning of virtual definition ). This most recent destructor calls the base destructor (that’s because of destructor definition ” when object goes out of scope, destructor calls ” ) before exiting . This way we are making sure all the dynamically allocated memory in the inheritance hierarchy get freed from top-down fashion.
A virtual constructor would much useful than useless that you all say. For example it would be very usefull when we want a copy of an object that you ‘don’t know’. But in C++ there is a little workaround, that I’ll show in the following sample:
class VCONA
{
public:
virtual VCONA *vc() const = 0; /*the workaround for a virtual constructor*/
virtual ~VCONA()
{
};
};
class VCONB: public VCONA
{
protected:
VCONB *vc() const
{
return new B(*this);
}
};
void dothis(VCONA &a)
{
VCONA *aCOPY = a.vc(); /*no idea what object ‘a’ is or where it came from, but copy it!*/
delete aCOPY; /*virtual destructor here!*/
}
int main()
{
VCONB
dothis(b);
return (0);
}
As you see, a virtual constructor would be a nicer way to achieve this (although I have no idea how such implementation should look like and how a compiler could handle this). However, you are right that this is a very rare situation
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.
I read all the comments given here. Here is my opinion.
virtual is used to achieve the dynamic polymorphism concept in inheritance hierarchy. By this keyword we are making sure that the latest copy of that function ( most Derived function ) should be called which already explained in the the previous discussion using Vtable concept. Coming back to the original point about virtual constructor.
1. Kindly note that virtual keyword only used with function declaration not with definition.
2. There is nothing like Constructor declaration. There is always a constructor definition used to initialize the variable ( general purpose ).
3. The address mapping for virtual function is done at runtime on the base of type of object only, which is actually created by constructor. Because virtual keyword is in declaration to tell compiler to keep latest address of the virtual function where the definition of function is store in compile form.
4. Again in inheritance hierarchy. to deal with the most derived object, all the data member of hierarchy level must be initialize (irrespective weather object is created dynamically). Again we know that class having incomplete definition can not create an instance). So in constructor definition of most derived class we also have to supply values to initialize base class data member so that base constructor also will be called.
5. in case of virtual destructor, The scene is quite different. Here by giving virtual keyword to the destructor you are calling destructor of most recent object. ( as I told you earlier in meaning of virtual definition ). This most recent destructor calls the base destructor (that’s because of destructor definition ” when object goes out of scope, destructor calls ” ) before exiting . This way we are making sure all the dynamically allocated memory in the inheritance hierarchy get freed from top-down fashion.
Kindly correct me if anywhere I am wrong..
Virtual constructors aren’t supported by most languages simply because there’s not enough demand.
If more people would demand them, then the language implementers would find a way to do it and I doubt it would involve the vtable.
After all, the compiler (C# anyway) can add a “default constructor”, it should be able to propogate virtual constuctors — at compile time.
A virtual constructor would much useful than useless that you all say. For example it would be very usefull when we want a copy of an object that you ‘don’t know’. But in C++ there is a little workaround, that I’ll show in the following sample:
class VCONA
{
public:
virtual VCONA *vc() const = 0; /*the workaround for a virtual constructor*/
virtual ~VCONA()
{
};
};
class VCONB: public VCONA
{
protected:
VCONB *vc() const
{
return new B(*this);
}
};
void dothis(VCONA &a)
{
VCONA *aCOPY = a.vc(); /*no idea what object ‘a’ is or where it came from, but copy it!*/
delete aCOPY; /*virtual destructor here!*/
}
int main()
{
VCONB
dothis(b);
return (0);
}
As you see, a virtual constructor would be a nicer way to achieve this (although I have no idea how such implementation should look like and how a compiler could handle this). However, you are right that this is a very rare situation
Leave an Answer/Comment