Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Destructor not called for inherited class through a template class

Reply
Thread Tools

Destructor not called for inherited class through a template class

 
 
iTooo
Guest
Posts: n/a
 
      11-05-2005
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
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      11-05-2005
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
 
Reply With Quote
 
 
 
 
iTooo
Guest
Posts: n/a
 
      11-05-2005
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.
 
Reply With Quote
 
maadhuu
Guest
Posts: n/a
 
      11-05-2005
hi,
I am sure that it solved the problem,but what you can do is virtualize the
constructor.
bye.

 
Reply With Quote
 
Peter_Julian
Guest
Posts: n/a
 
      11-05-2005

"maadhuu" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
how to call an inherited, template class constructor from initializerlist of an inheriting, non-template class constructor l.s.rockfan@web.de C++ 4 11-15-2008 01:22 PM
how to call an inherited, template class constructor from initializerlist of an inheriting, non-template class constructor l.s.rockfan@web.de C++ 2 11-14-2008 10:04 PM
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 2 05-20-2008 02:20 AM
newbie -- smart pointer destructor called without destructor everbeing called Jimmy Hartzell C++ 0 05-19-2008 07:05 PM
'Class.inherited' v. 'inherited' syntax inside Class 7stud -- Ruby 11 11-09-2007 06:45 PM



Advertisments