In c++ there is only virtual destructors, no constructors. Why?
Answer Posted / 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 |
Post New Answer View All Answers
Can a destructor be called directly?
Advantage and disadvantage of routing in telecom sector
What is the full form of oops?
What is Difeerence between List obj=new ArrayList(); and ArrayList obj=new ArrayList()?
Can we have inheritance without polymorphism?
Can you explain polymorphism?
Why is it so that we can have virtual constructors but we cannot have virtual destructors?
What are the components of marker interface?
String = "C++ is an object oriented programming language.An imp feature of oops is classes and objects".Write a pgm to count the repeated words from this scenario?
What is the advantage of oop over procedural language?
What are benefits of oop?
They started with the brief introduction followed by few basic C++ questions on polumorphism, inheritance and then virtual functions. What is polymorphims? How you will access polymorphic functions in C? How virtual function mechanism works?
write string class as your own class in java without using any built-in function
What is overloading in oops?
What is encapsulation example?