|
-
August 14th, 2011, 01:15 PM
#1
virtual destructor
What if I don't declared virtual for my destructor ?
class Base
{
public: virtual void func()=0;
};
class Derived ublic Base
{
public:void func(){//dosomething}
};
????
Thank you.
-
August 14th, 2011, 01:38 PM
#2
Re: virtual destructor
 Originally Posted by Maejie
What if I don't declared virtual for my destructor ?
Then if you ever use delete or delete[] on a Base pointer that points to a Derived object, you get undefined behaviour.
-
August 15th, 2011, 07:15 AM
#3
Re: virtual destructor
In addition to what laserlight has written, any class you derive from should have a public virtual destructor (implicit from further base classes or explicitly marked as such):
e.g.
Code:
struct Base
{
virtual ~Base(){}
};
struct Middle : Base {};
struct Derived : Middle {};
int main()
{
Base b* = new Derived();
delete b; //OK, nothing will be leaked because Base is explicitly
//marked virtual
Middle m* = new Derived();
delete m; //OK, nothing will be leaked because the destructor
//of Middle is implicitly virtual because Middle derives
//from Base, and Base has a virtual destructor
Derived d* = new Derived();
delete d //OK, nothing will be leaked, deleted from derived pointer.
}
or a protected destructor
Code:
struct Base
{
protected:
~Base(){}
};
struct Derived {};
int main()
{
Base b* = new Derived();
delete b; //Compile error, Derived must be deleted from a derived
//pointer (should dynamic cast the base pointer to the
//derived pointer and then delete via the derived pointer.
Derived d* = new Derived();
delete d; //OK, nothing will be leaked, deleted from derived pointer.
}
If you use protected inheritance you should be very careful if the protected destructor is not marked virtual... consider what happens in the following code...
Code:
struct Base
{
protected:
~Base(){}
};
struct Middle : Base {};
struct Derived {};
int main()
{
Base b* = new Derived();
delete b; //Compile error, Derived must be deleted from a derived
//pointer (should dynamic cast the base pointer to the
//derived pointer and then delete via the derived pointer.
Derived d* = new Derived();
delete d //OK, nothing will be leaked, deleted from derived pointer.
Middle m* = new Derived();
delete m; //No compile error but the Derived section of the
//allocation will not be deleted.
}
If you cannot modify the Base class, the problem in the above code can be solved by making the destructor of Middle virtual:
Code:
struct Middle : Base
{
virtual ~Middle(){}
};
Anyway, I would advise using public inheritance.
Last edited by PredicateNormative; August 16th, 2011 at 05:33 AM.
Reason: grammar
-
August 15th, 2011, 02:33 PM
#4
Re: virtual destructor
Thank you soooooo very much for your helpful reply
But in the third code block,
Code:
delete m; //No compile error but the Derived section of the
//allocation will no be deleted
How could you know that ?
-
August 15th, 2011, 02:58 PM
#5
Re: virtual destructor
 Originally Posted by Maejie
Thank you soooooo very much for your helpful reply
But in the third code block,
Code:
delete m; //No compile error but the Derived section of the
//allocation will no be deleted
How could you know that ?
Let's give you an easy example:
Code:
class Base
{
public:
char *p;
Base() { p = new char [10]; }
~Base() { delete [] p; }
};
class Derived : public Base
{
public:
char *ptr;
Derived() { ptr = new char [10]; }
~Derived() { delete [] ptr; }
}
int main()
{
Base *pb = new Derived;
delete pb; // behaviour is undefined
}
If you are deleting a derived object through a base class pointer, and the base class does not have a virtual destructor, the behaviour is undefined.
The above code may result in a memory leak, maybe not -- that's what is meant by undefined behaviour. To ensure that both derived and parent are destroyed, the base class's destructor must be virtual.
Edit: Changed name of derived pointer.
Regards,
Paul McKenzie
Last edited by Paul McKenzie; August 15th, 2011 at 03:36 PM.
-
August 15th, 2011, 03:15 PM
#6
Re: virtual destructor
It may be less confusing if Derived had a pointer of a different name than Base as a member.
-
August 15th, 2011, 04:05 PM
#7
Re: virtual destructor
Thank you Paul and Lindley, I understand virtual destructor better now.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|