By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,445 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,445 IT Pros & Developers. It's quick & easy.

Destructor not called for inherited class through a template class

P: n/a
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
Nov 5 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
iTooo wrote:
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
Nov 5 '05 #2

P: n/a
John Harrison a écrit :
iTooo wrote:
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


Thank you very much, I didn't knew destructors could be virtual as
constructors cannot.
Nov 5 '05 #3

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

Nov 5 '05 #4

P: n/a

"maadhuu" <ma************@yahoo.com> wrote in message
news:f1******************************@localhost.ta lkaboutprogramming.com...
| 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.
Nov 5 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.