C++/c++

Advertisement


Question
what is virtual constructor & virtual distrutor?
Is virtual constructor exixsts?

Answer
swapnil gujrathi , Thank you for your question.

A virtual destructor is just like a normal destructor except that it is executed when a derived class is cast to its base class, as shown below. There is no such thing as a virtual constructor.

class Base
{
}; // class Base

class Derived : public Base
{
virtual ~Derived()
{
// virtual destructor in derived class
} // ~Derived
}; // class Derived

Base * p;
p = new Derived;
// p is a Derived object treated as though it were a Base object
delete p; // Executes the virtual destructor in Derived
// If the destructor were not virtual it would not be executed

David Spector

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


David Spector

Expertise

Highly knowledgeable in the C++ language, Visual C++ (MSVC), Windows API, documentation and other quality-assurance techniques, and debugging. Knowledgeable in MFC, COM, GUI design, and object-oriented design.

Experience

I have been a software engineer since 1965. I have been published. My specializations have been: biomedical programming, compiler implementation, and many kinds of Windows programming. I don't do Databases or other business-oriented stuff.

Publications
Windows?/DOS Developer's Journal, ACM SIGPLAN Notices, and Computer Science Press.

Education/Credentials
ICCP Systems Programming Certification
Master's degree equivalent in Computer Science

©2016 About.com. All rights reserved.