You are here:

C++/virtual constructor and destructor

Advertisement


Question
sir/madam
pls explain me the following

1. Can we have "Virtual Constructors"?
2. Explain the need for “Virtual Destructor”.

Answer
Hello John, thank you for the question.

1. No, you cannot have a virtual constructor. Declaring a constructor virtual will result in a compile error.

2. Virtual destructors are commonly used with inheritance. Since an abstract class must contain a pure virtual method that has to be overridden, a lot of developers commonly declare their destructors pure virtual, since a destructor is sure to be implemented in every subclass. Declaring a method as virtual allows the compiler to figure out the appropriate version to call on derived classes at runtime.

I hope this information was helpful.

- Eddie

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Eddie

Expertise

I can answer questions about the C++ language, object oriented design and architecture. I am knowledgable in a lot of the math that goes into programming, and am certified by ExpertRating.com. I also know a good deal about graphics via OpenGL, and GUIs.

Experience

I have completed numerous games and demos created with the C++ programming language. Currently employed as a software engineer in the modeling and simulation field. I have about 7 years experience.

©2016 About.com. All rights reserved.