-
May 4th, 2011, 10:03 AM
#1
inheritnce
Mammal *pDog = new Dog;
here Mammal is base class and Dog is it's derived class
In such declaration destructor is not called why is it rule of C++?
-
May 4th, 2011, 10:12 AM
#2
Re: inheritnce
Originally Posted by vkash
Mammal *pDog = new Dog;
here Mammal is base class and Dog is it's derived class
In such declaration destructor is not called why is it rule of C++?
I don't understand your question. What do you mean by "the destructor is not called"?
You created a Dog object dynamically and assign it to a base class pointer. That's all that code does. Is it supposed to do something more than that?
Regards,
Paul McKenzie
-
May 4th, 2011, 10:25 AM
#3
Re: inheritnce
Originally Posted by Paul McKenzie
I don't understand your question. What do you mean by "the destructor is not called"?
You created a Dog object dynamically and assign it to a base class pointer. That's all that code does. Is it supposed to do something more than that?
Regards,
Paul McKenzie
OK compile this ode in visual c++
#include <iostream>
using std::cout;
class Mammal
{
public:
Mammal():itsAge(1) { cout << "Mammal constructor...\n"; }
~Mammal() { cout << "Mammal destructor...\n"; }
void Move() const { cout << "Mammal move one step\n"; }
void Speak() const { cout << "Mammal speak!\n"; }
protected:
int itsAge;
};
class Dog : public Mammal
{
public:
Dog() { cout << "Dog Constructor...\n"; }
~Dog() { cout << "Dog destructor...\n"; }
void WagTail() { cout << "Wagging Tail...\n"; }
void Speak()const { cout << "Woof!\n"; }
void Move()const { cout << "Dog moves 5 steps...\n"; }
};
int main()
{
Mammal *pDog = new Dog;
pDog->Move();
pDog->Speak();
return 0;
}
mammal and dog constructor called but their destructor not called at the end of main that is what i want to ask you
t
-
May 4th, 2011, 10:32 AM
#4
Re: inheritnce
mammal and dog constructor called but their destructor not called at the end of main that is what i want to ask you
Code:
Mammal *pDog = new Dog;
You aren't deleting pDog.
Now the descructor will be called. Remember, C++ doesn't do garbage collection like Java does.
-
May 4th, 2011, 10:33 AM
#5
Re: inheritnce
Originally Posted by vkash
mammal and dog constructor called but their destructor not called
That is because in C++, you must call "delete" for every call to "new" you make.
Secondly, your base class destructor must be virtual for the delete to work correctly.
Regards,
Paul McKenzie
-
May 4th, 2011, 10:33 AM
#6
Re: inheritnce
Originally Posted by vkash
mammal and dog constructor called but their destructor not called at the end of main that is what i want to ask you
You are responsible for deleting objects that you allocate.
But my guess is that you will have another question after you add
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
May 4th, 2011, 11:36 AM
#7
Re: inheritnce
Inheritance has nothing to do with this. Every new must have a matching delete, no matter whether the types being created are related by inheritance or not.
-
May 10th, 2011, 12:03 AM
#8
Re: inheritnce
thanks to all of you i got the point that is -> I have created pDog so i am responsible for their deletion destructor will called when i write delete pDog; in my code.
once again thanks to all of you.
-
May 10th, 2011, 02:24 AM
#9
Re: inheritnce
Hi,
Just to clarify, as Paul pointed out the Dog destructor will only be called if the Mammal destructor is declared as virtual (which it isn't in the code that you posted.)
Alan
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|