473,379 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,379 software developers and data experts.

Destructor not called for inherited class through a template class

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
4 3855
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
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
hi,
I am sure that it solved the problem,but what you can do is virtualize the
constructor.
bye.

Nov 5 '05 #4

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Virendra Verma | last post by:
This sounds weird, but I am looking for separate behaviors for destruction of a const and non-const object. I am trying to develop a smart/auto pointer class for writing objects to disk...
8
by: Tony Johansson | last post by:
Hello Experts! I have one easy question for you. If I have an abstract class is it any point to define a destructor with some code within the body because an abstrct class can not be...
4
by: Tony Johansson | last post by:
Hello Experts!! Assume I have a base class called animal. I want this class to be abstract so I make the destructor pure virtual by having this statement. virtual ~Animal() = 0; Destructor...
9
by: Daniel Kay | last post by:
Hello! I have written two template classes which implement the observerpattern in C++. I hope I manage to desribe the problem I have. template<class T> class Observer { /* ... */ }; ...
6
by: Squeamz | last post by:
Hello, Say I create a class ("Child") that inherits from another class ("Parent"). Parent's destructor is not virtual. Is there a way I can prevent Parent's destructor from being called when a...
1
by: Karine Pinault | last post by:
I am having trouble trying to compile existing code with the /clr flag. The following error occurs and I can't figure it out how to fix this problem. I have the following declaration:...
7
by: Lloyd Dupont | last post by:
I create 2 ManagedC++ class inherithing from each other. I can't see how do I call super destructor or ensure it's called :-/ template <class T> public ref class CArray : System::IDisposable {...
3
by: nick4ng | last post by:
If I have a templated constructor, there is a way to know, at compile time, the instance when compiling the destructor? To be clearer my problem is that I have a templated constructor that is...
7
by: Tonni Tielens | last post by:
I'm trying to create a pure virtual class describing an interface. Normally, when I do this I make the destructor pure virtual so that, even if there are no members in the class, it cannot be...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.