In c++ there is only virtual destructors, no constructors. Why?
Answers were Sorted based on User's Feedback
Answer / uma sankar pradhan
constructors are invoked at the time of object creation.
The v-table for virtual function mechanism is not
intialised properly during the constructor invocation.
So constructors cannot be virtual
But,destructors can be virtual.Because the V-table is
properly initialised at the time of object creation.
So the v-table is available for the destructors.
Destructors can be made pure virtual also.But unlike other
member functions,it must be redfined outside the class.
Is This Answer Correct ? | 57 Yes | 22 No |
Answer / bala preethi
Since OOPS basics says always base class object to be
created and than its derivatives. So just consider the case
where we try to create object of derived class with virtual
contructor since pointer points to derived type object it
fetches derived constructor directly, without its base
class object constructed( instantiated )...
So Constructor cannot be made virtual bt when comes to case
of Destructor object destuction process must be reverse...
So we must make destructor as virtual.
This applies only when we going for Dynamic Memory
Allocation...
If, on the other hand, you were to have a pointer to object
A and assign it a dynamic object B, when you destroy it,
only object A's destructor is called and object B part of
the object is never destroyed. Virtual destructors are used
to solve this problem and destroy the objects in the right
order.
Is This Answer Correct ? | 47 Yes | 14 No |
Answer / subhashish sen
Constructor can not be virtual...The main reason is when we
have a virtual function in a class the VTABLE for that
function will be created..which contains the address of the
virtual function...means for each class which contains a
virtual function a VTABLE will be created and constructor is
an entity which initializes this VTABLE.So if you declare
constructor as virtual who will initialize VTABLE for it.
Destructors can be virtual because virtual destructor
ensures proper calling order of all destructors in a class
hierarchy.
If you need further clarification reply me....
Is This Answer Correct ? | 28 Yes | 9 No |
Answer / jos collin
The VTable is created at compile-time itself. But properly
initialized at runtime, including vptr.
Virtual Mechanism is possible only with completely
constructed objects. The constructor is called when the
object is not created completely, it is not allowed.
Is This Answer Correct ? | 0 Yes | 6 No |
Can private class be inherited?
tel me oops defination in single line
What is overloading in oops?
Differences between inline functions and non-inline functions?
write a program which best describes polymorphism in real world?
2 Answers CTS, Infosys, NexGen,
What is Inheritance, Multiple Inheritance, Shared and Repeatable Inheritance?
What are virtual functions?
Get me a number puzzle game-program
what is the difference between javap and jad utility
what is new operator in c++
Why do we need polymorphism in c#?
What is destructor oops?