Solved

Pure virtual Destructor

Posted on 2000-04-30
7
648 Views
Last Modified: 2008-02-01
Can i have an explanation on the concept of Pure Virtual Destructor and its usage and syntax?.
0
Comment
Question by:TSENTHILKLUMAR
7 Comments
 
LVL 2

Accepted Solution

by:
Nevyn earned 50 total points
Comment Utility
You use a virtual destructor to make sure that sub-classes and parent classes are destroyed properly. One virtual destructor will call another, but I don't think that a standard destructor will call up or down the parent class chain. A pure virtual destructor is just to signify that all sub-classes should use virtual destructers, but this class has no implementation for it.
I'm not to sure on this, so I could bew rong, but I think from some experiments I ran, a standard destructor won't call super/sub class destructors.
0
 

Author Comment

by:TSENTHILKLUMAR
Comment Utility
the explanation given is not apt.please give me some other explanations.
0
 
LVL 22

Expert Comment

by:nietod
Comment Utility
>> the explanation given is not apt.please
>> give me some other explanations.
You've accepted this answer.  If you want more help, you should request it without grading the answer.  Accept an answer only when the question is sufficiently answered.  Since the question has been graded, there is little insentive for experts to assist with it, since they cannot get the points.  In addition, they have to PAY points to even look at the question.   So it is unusually get any new help once a question is graded  (I saw that this was given a "D", which is why I looked into it.)

a pure virtual destructor is three concepts layered on each other.  It is best to understand all three seperately.

First of all a destructor is a function that is autmatically called when an object is about to be destroyed (removed from memory, removed from "existance").  The destructor is responsible for performing clean-up operations for the object, like deleting memory it has allocated, closing files it may be maintaining and many other possible things.  Are you familar with destructors?

In a class herarchy, a derived class may override a base class's function.  That means that the derived class has provided an alternate implimentation to a function that was originally defined in the base class.  For example,

class Base
{
public:
    char F() { return 'B'; };
};

class Derived : public Base
{
public:
    char F() { return 'D'; };
};

the class Derived has overriden the base class's F() to make it return a different letter.

Usually the compiler chooses to call the base class version or the derived class version of the function based on the compile-time information about the object.  So for example

Base B;
Derived D;

char C1 = B.F(); // Set to 'B'.
char C2 = D.F(); // Set to 'D'.

However, one important feature in OOP is that you can have code that works with a reference to an object, where that object may be of a type derived from the type specified in the code.  In this case the compiler "thinks" that you are working with a base class object, but you might actually be working with any object derived from that base class, for example,

Base *Ptr;
Ptr = &B;                 // Point to base object.
char C3 = Ptr->F(); // Set to 'B".  
Ptr = &D;                 // Point to derived object.
char C4 = Ptr->F(); // STILL Set to 'B", even though derived was used.

In this case the actual object being used is a Derived class.  But the compiler looks at the type of pointer being used and sees that the it is a pointer to a base class, so even though the actual object is a derived class object, the compiler calls the base class version of the function.   Sometimes that is what we want.  But sometimes not.

When it is isn't what we want, we need to use virtual functions.  With a virtual function, the program looks at the object's actual type at run-time and determines which function to call at that time.  For example

class Base
{
public:
    virtual char F() { return 'B'; };
};

class Derived : public Base
{
public:
    virtual char F() { return 'D'; };
};

Base *Ptr;
Ptr = &B;                 // Point to base object.
char C5 = Ptr->F(); // Set to 'B".  
Ptr = &D;                 // Point to derived object.
char C6 = Ptr->F(); // Set to 'D'

Now to put together virtual and destructor.  When you delete an object the object's destructor must be called.  Just as above you can delete the object tha is actually of a derived class using a pointer to a base class, like

Base *Ptr = new Derived;
delete Ptr;

In this case, if the destructor is not virtual, the compiler will, just as before, use the base class version of the desructor function.  This means that if there were operations that need to be performed by the derived class destructor, those operations will not be performed.  To prevent that from happening, you can declare the destructor to be virtual.  Then the compiler will choose the destructor to be called at run-time, based on the actual type of object being destroyed.   It is often very important that the compiler choose the destructor to go with the actual object being destroyed.  So in most class hierarchies, the destructors are declared as virtual.

Now pure. When a class contains one or more pure functions, the class is considered "abstract" (as opposed to "concrete").  This means that no objects f the class can ever be created.  Now classes may be derived from the abstract classes and it may be possible to create objects of these classes if they are made "concrete"  To make a class concrete you must provide definitions for all of its base class's pure virtual functions.  To make a function pure you need to place a "= 0" at the end if its declaration, like

class Base
{
public:
    virtual char F() = 0; // pure virutal function
};

class Derived : public Base
{
public:
    virtual char F() { return 'D'; }; // definition for pure virtual function.
};

Base B; // ERROR.  Base is abstract.
Derived D; // OKAY Derived is concrete.

Now to put pure and virtual destructor together.  If you want to make a class abstract, you need to give it at least one pure virtual function.  In many cases the best choice for this function is the destructor.  This is simply because almost all class hierarchies will have a virtual destructor and may not have other virtual functions to choose from.
0
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Expert Comment

by:vmehro
Comment Utility
Excellent Overview....
0
 

Author Comment

by:TSENTHILKLUMAR
Comment Utility
the explanation wasquite apt.
0
 

Expert Comment

by:premadhas
Comment Utility
That was too informative Nietod
KPN be careful to grade in future
Regards,
Premadhas
0
 
LVL 5

Expert Comment

by:Wyn
Comment Utility
EE is cool
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

6 Experts available now in Live!

Get 1:1 Help Now