?
Solved

Pure virtual Destructor

Posted on 2000-04-30
7
Medium Priority
?
658 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 

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

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

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

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…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
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…
Suggested Courses

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