You are here:

C++/constructor

Advertisement


Question
What is the need for virtual destructor?
Can we have virtual constructor?


Answer
you need virtual destructor, when there is acase where someone would dlete a drived object, using the base class pointer. For instance, if p points to SHAPE object and q points to CIRCLE object that is drived from SHAPE. Then if you have "p=(SHAPE)q" and then you use "delete p", it will call destructor corresponding to q.
In C++ you cannot have virtual constructor, and usually you don't need it. In rare cases that you might need such functionality, you can simulate it using paramerets of the constructor.

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Sam

Expertise

You may ask me about simple to complicated C/C++ programming methods/style and most like questions about network programming in C++.

Experience

I'm a professional programer in C++ and network/internet programming.

©2016 About.com. All rights reserved.