Click here to Skip to main content
15,886,530 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
hello,
I have one base class
C++
class Base{
    public:
        Base();
        ~Base();
}


And one child class
C++
   class Child : public Base
   {
       public:
           Base();
           ~Base();
   }

int main()
{
Child *c = new Child();
Base *b = c;
delete b;
return 0;
}


In this code , Base class destructor is called. I want to call Child class Destructor. How is it possible?
Posted
Updated 10-Mar-12 23:09pm
v3
Comments
Sergey Alexandrovich Kryukov 11-Mar-12 16:18pm    
The class Child is not a "child" class, it is a derived class.
--SA

1 solution

Define the destructor with the virtual keyword:


C++
class Base{
public:
Base();
virtual ~Base();
}

Another issue: The constructor and destructor of the Child class, should be named Child() and virtual ~Child() (instead of Base() and ~Base()).

 
Share this answer
 
v3
Comments
Nelek 11-Mar-12 7:51am    
If you don't mind try to avoid abbreviations in the answers, not all people are familiar with them. We like correctly written questions, so we should try to give correctly written answers :)
Sergey Alexandrovich Kryukov 11-Mar-12 16:20pm    
What do you mean by "shortcuts"? Abbreviations, perhaps? Then I would totally agree. Most abbreviations are not acceptable, except for few very well-known ones.
--SA
Nelek 11-Mar-12 16:35pm    
Thanks for the correction. You are right, I wanted to say abbreviations. Edited
Shmuel Zang 12-Mar-12 2:36am    
Thank you for the comment. I updated the solution appropriately.
Espen Harlinn 12-Mar-12 19:29pm    
5'ed!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900