Destructor not called for inherited class through a template class

I

iTooo

What a subject, mhhh not resumable. Here is my problem:

I'm using a template class for lists, upon deletion it deletes its
elements, ok, works fine.
Trouble comes when I put in this list inherited classes and the list is
templated for the parent class. When the list deletes its elements, the
destructor called is not the one of the classes, but their parent's.

Here is a little example code

Class definitions:
--
class ParentClass
{
};

class NotDeleted : public ParentClass
{
public:
~NotDeleted() { printf("I was deleted\n"); }
};

template <class T> class TplClass
{
public:
void DeleteElement(T *elem) { delete elem; }
};
--


Code:
--

NotDeleted *nd = new NotDeleted();
ParentClass *pc = (ParentClass *)nd;
delete nd;

--> Prints "I was deleted";

NotDeleted *nd = new NotDeleted();
TplClass<ParentClass> tpl;
tpl.DeleteElement(nd);

--> Does NOT print anything

--

The only way I could explain this behaviour is the loss of the instance
"real" class when using the template class. Is there a way to work
around with it ?

Thanks for you help
 
J

John Harrison

iTooo said:
What a subject, mhhh not resumable. Here is my problem:

I'm using a template class for lists, upon deletion it deletes its
elements, ok, works fine.
Trouble comes when I put in this list inherited classes and the list is
templated for the parent class. When the list deletes its elements, the
destructor called is not the one of the classes, but their parent's.

Here is a little example code

Class definitions:
--
class ParentClass
{
};

class NotDeleted : public ParentClass
{
public:
~NotDeleted() { printf("I was deleted\n"); }
};

template <class T> class TplClass
{
public:
void DeleteElement(T *elem) { delete elem; }
};
--


Code:
--

NotDeleted *nd = new NotDeleted();
ParentClass *pc = (ParentClass *)nd;
delete nd;

--> Prints "I was deleted";

NotDeleted *nd = new NotDeleted();
TplClass<ParentClass> tpl;
tpl.DeleteElement(nd);

--> Does NOT print anything

--

The only way I could explain this behaviour is the loss of the instance
"real" class when using the template class. Is there a way to work
around with it ?

Thanks for you help

The problem is solved by declaring a virtual destructor in your parent
class.

class ParentClass
{
public:
virtual ~ParentClass() {}
};

You should always do this is a base class if there is any possibilty of
wanting to delete a derived class object through a pointer to the base
class.

john
 
I

iTooo

John Harrison a écrit :
The problem is solved by declaring a virtual destructor in your parent
class.

class ParentClass
{
public:
virtual ~ParentClass() {}
};

You should always do this is a base class if there is any possibilty of
wanting to delete a derived class object through a pointer to the base
class.

john

Thank you very much, I didn't knew destructors could be virtual as
constructors cannot.
 
M

maadhuu

hi,
I am sure that it solved the problem,but what you can do is virtualize the
constructor.
bye.
 
P

Peter_Julian

| hi,
| I am sure that it solved the problem,but what you can do is virtualize
the
| constructor.
| bye.
|

In C++, there is no such thing as a virtual constructor.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top