General discussion
-
CreatorTopic
-
September 14, 1999 at 11:34 pm #2080351
What is a virtual destructor?
Lockedby brrjnessel · about 25 years, 7 months ago
Hi,
Does anybody know why you would use a virtual destructor?
Thanx,
John N.Topic is locked -
CreatorTopic
All Comments
-
AuthorReplies
-
-
September 21, 1999 at 1:14 pm #3901194
What is a virtual destructor?
by kevin · about 25 years, 7 months ago
In reply to What is a virtual destructor?
The simple answer is that a virtual destructor is necessary if you wish to destroy (using operator delete) a dynamically allocated object (allocated with operator new), using a _pointer to a BASE CLASS of that object_. The virtual destructor ensuresthe correct destructor sequence is called, and the object is cleaned up correctly, even if you are calling delete using a base-class pointer.
IT IS MY RECOMMENDATION THAT you _ALWAYS_ code your destructors as VIRTUAL if your class contains ANY virtual functions. This will avoid pernicious bugs creeping into your code that can be quite difficult to find.
It’s hard to gauge your experience level from the question so my apologies if I go over obvious stuff from hereon:
Here’s a [relatively] simple example (I assume you understand virtual functions):
Let’s say you’re implementing a simple shape drawing program, and the base class for all shapes is cShape. cShape contains an x,y coordinate and a virtual function called Paint (), which draws the shape. Y
-
September 20, 2000 at 7:01 pm #3793499
What is a virtual destructor?
by brrjnessel · about 24 years, 7 months ago
In reply to What is a virtual destructor?
The question was auto-closed by TechRepublic
-
-
September 28, 1999 at 5:21 am #3901094
What is a virtual destructor?
by ram_cv · about 25 years, 6 months ago
In reply to What is a virtual destructor?
Dear John,
Let me just explain this to you with an example :
Let’s take three classes :
Base , Derived1, Derived2.Derived1 and Derived2 are derived from Base.
Now suppose Base has a non virtual destructor. Now supposing a pointer to baseis declared and it is currently pointing to a derived1 object. Now we delete this pointer, what will happen is the destructor of Base will be called. But actually OOPS emphasizes that the destructor of Derived1 should be called as currently the pointer is pointing to Derived1’s object. Similarly if the object is pointing to Derived2 then that destructor should be called.
Now if we had the Base Destructor as Virtual and override it with the Destructors in the Derived classes then on destructingthe Derived object the suitable Destructor will be called.
Hence whenever you inherit a class a Virtual Destructor is a very good programming practice.
I hope this helps you. You can also refer to Sams Publications Teach Yourself C++ in 21 Days byJesse Liberty. It is-
September 20, 2000 at 7:01 pm #3793500
What is a virtual destructor?
by brrjnessel · about 24 years, 7 months ago
In reply to What is a virtual destructor?
The question was auto-closed by TechRepublic
-
-
October 24, 1999 at 11:34 pm #3900857
What is a virtual destructor?
by coop cabo rojo · about 25 years, 6 months ago
In reply to What is a virtual destructor?
Probably this will help you:
1. Pure virtual destructors are an interesting aspect of C++. If a function is pure virtual, then the class that contains it is abstract,and C++ will not allow it to be instantiated.
Purity is inherited. Thus:
struct B {virtual void f() = 0;};
struct D : B {}; // abstract class, pure virtual f is inherited.However, destructors are *not* inherited. Thus:
struct B {virtual ~B() = 0;};
struct D : B {} // concrete class. Compiler generates destructor.All classes must have a destructor. If one is not specified, then the
compiler will write one for you. If your destructor is pure virtual,
you still have to supply an implementation.Thus:
struct B {virtual ~B() = 0;}
B::~B(){/* do whatever */}If you don’t implement the pure virtual destructor, you will get a
link error.————————————-
What all this adds up to is this. A pure virtual destructor is exactly like a regular virtual destructor,
-
September 20, 2000 at 7:01 pm #3793501
What is a virtual destructor?
by brrjnessel · about 24 years, 7 months ago
In reply to What is a virtual destructor?
The question was auto-closed by TechRepublic
-
-
April 18, 2000 at 9:17 pm #3899741
What is a virtual destructor?
by lastorck · about 25 years ago
In reply to What is a virtual destructor?
Look:
class Error
{
private:
char* ErrStr;
public:
Error(char* str)
{
ErrStr = new char(strlen(str)+1);
strcpy(ErrStr, str);
}
virtual ~Error(){delete ErrStr;}
virtual Put()
{
printf(“%s\n”, ErrStr);
}
};
class ErrWithDescr
{
private:
char* Descr;
public:
ErrWithDescr(char* str, char* ds )
: Error(str)
{
Descr = new char(strlen(ds)+1);
strcpy(Descr, ds);
}
virtual ~ErrWithDescr(){delete ErrStr; delete Descr;}
virtual Put()
{
printf(“%s : %s\n”, ErrStr, Descr);
}
};void ShowAndDeleteError(Error* err)
{
err->Put();
delete err;
}void main()
{
//…
Error* e1 = new Error(“Error1”);
Error* e2 = new ErrWithDescr(“Error2”, “Sm description”);ShowAndDeleteError(e1);
ShowAndDeleteError(e2); //use ~ErrWithDescr();
}
It maybe useful for exception objects, objects wich are used in conteiner that own it, … .-
September 20, 2000 at 7:01 pm #3793502
What is a virtual destructor?
by brrjnessel · about 24 years, 7 months ago
In reply to What is a virtual destructor?
The question was auto-closed by TechRepublic
-
-
September 20, 2000 at 7:01 pm #3793498
What is a virtual destructor?
by brrjnessel · about 24 years, 7 months ago
In reply to What is a virtual destructor?
This question was auto closed due to inactivity
-
-
AuthorReplies