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 |
What type of Job you are providing?
pointers are support in C#? if yes then how to use it?
8 Answers Softvision Solution,
Should you protect the global data in threads? Why or why not?
explain dynamic binding by drowing
What is constructor overloading in oop?
What is virtual destructor? Why?
3 Answers Agile Software, College School Exams Tests, CSC,
inheritence with example
Why is it so that we can have virtual constructors but we cannot have virtual destructors?
What are callback functions in c++
what i oops concept, how many languages supports oops concept?
Write a c++ program to display pass and fail for three student using static member function
What is interface? When and where is it used?