Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Pure virtual Destructor

Posted on 2000-04-30
7
Medium Priority
?
661 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
Suggested Courses

610 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