CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: inheritnce

  1. #1
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Post 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++?

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: inheritnce

    Quote Originally Posted by vkash View Post
    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

  3. #3
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    Re: inheritnce

    Quote Originally Posted by Paul McKenzie View Post
    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

  4. #4
    Join Date
    Sep 2004
    Location
    Holland (land of the dope)
    Posts
    4,123

    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.

    Code:
    delete pDog
    Now the descructor will be called. Remember, C++ doesn't do garbage collection like Java does.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449

    Re: inheritnce

    Quote Originally Posted by vkash View Post
    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

  6. #6
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,656

    Re: inheritnce

    Quote Originally Posted by vkash View Post
    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
    Code:
    delete pDog;
    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...

  7. #7
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    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.

  8. #8
    Join Date
    Mar 2011
    Location
    Delhi India
    Posts
    110

    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.

  9. #9
    Join Date
    Aug 2008
    Location
    Scotland
    Posts
    379

    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
  •  





Click Here to Expand Forum to Full Width

Featured