Destructor not called for inherited class through a template class

Discussion in 'C++' started by iTooo, Nov 5, 2005.

  1. iTooo

    iTooo Guest

    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
     
    iTooo, Nov 5, 2005
    #1
    1. Advertising

  2. Re: Destructor not called for inherited class through a templateclass

    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
     
    John Harrison, Nov 5, 2005
    #2
    1. Advertising

  3. iTooo

    iTooo Guest

    Re: Destructor not called for inherited class through a templateclass

    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.
     
    iTooo, Nov 5, 2005
    #3
  4. iTooo

    maadhuu Guest

    Re: Destructor not called for inherited class through a template

    hi,
    I am sure that it solved the problem,but what you can do is virtualize the
    constructor.
    bye.
     
    maadhuu, Nov 5, 2005
    #4
  5. iTooo

    Peter_Julian Guest

    Re: Destructor not called for inherited class through a template

    "maadhuu" <> wrote in message
    news:...
    | 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.
     
    Peter_Julian, Nov 5, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Douglas Peterson
    Replies:
    11
    Views:
    602
    Douglas Peterson
    Apr 26, 2005
  2. frs
    Replies:
    20
    Views:
    785
    Alf P. Steinbach
    Sep 21, 2005
  3. Jimmy Hartzell
    Replies:
    0
    Views:
    437
    Jimmy Hartzell
    May 19, 2008
  4. Jimmy Hartzell
    Replies:
    2
    Views:
    1,181
    Jimmy Hartzell
    May 20, 2008
  5. 7stud --
    Replies:
    11
    Views:
    449
    7stud --
    Nov 9, 2007
Loading...

Share This Page