Solved

Pure virtual Destructor

Posted on 2000-04-30
7
651 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
ID: 2765346
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
ID: 2765403
the explanation given is not apt.please give me some other explanations.
0
 
LVL 22

Expert Comment

by:nietod
ID: 2765603
>> 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

Expert Comment

by:vmehro
ID: 2766550
Excellent Overview....
0
 

Author Comment

by:TSENTHILKLUMAR
ID: 2767988
the explanation wasquite apt.
0
 

Expert Comment

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

Expert Comment

by:Wyn
ID: 2768724
EE is cool
0

Featured Post

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.

777 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