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

    HELP! delete function using base-class pointer

    Too tired to look this up right now...

    Can a base-class ptr be used to delete an object of a derived class?

    for instance (CMyWnd is derived from CWnd):

    CMyWnd* pMyWnd = new CMyWnd; // create derived obj on heap
    CWnd* pWnd = pMyWnd; // point base class ptr to obj of derived class
    delete pWnd; // delete obj of derived class

    this seems to work, but is it a clean deletion? For instance, if CMyWnd has additional data items (takes up more mem) will the CWnd* deletion remove the entire CMyWnd class structure?

    Thanx for any help. I have a feeling that I've actually just read the answer to this, but right now everything is just falling out my ears.

    Niall



  2. #2
    Join Date
    May 1999
    Posts
    123

    Re: HELP! delete function using base-class pointer

    Yes, it's legal as long as the base class contains a virtual destructor.


    The universe is a figment of its own imagination.

  3. #3
    Guest

    Re: HELP! delete function using base-class pointer

    above code deletes the derived object by calling derivd class destructor since destrcctor os CWnd class is virtual member. derived class destructor should do cleening process



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