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

Thread: c++ question

  1. #1
    Join Date
    Apr 1999
    Posts
    3

    c++ question

    The following code snippet works although my destructor of class B is declared private. Whereas, if I do not derive my class B virtual the following program fails to compile. Why? I am using VC++ 5.0.

    class A {

    public:
    A() {
    cout<<"\nIn constructor of A";
    }

    ~A() {
    cout<<"\nIn destructor of A";
    }
    };

    class B : virtual public A {

    public:
    B() {
    cout<<"\nIn Constructor of B";
    }
    private:
    ~B() {
    cout<<"\nIn Destructor of B";
    }
    };

    void main(void)
    {
    B b;
    }




  2. #2
    Join Date
    Apr 1999
    Posts
    90

    Re: c++ question

    The destructors need to be virtual, as in:

    Class A
    {
    public:
    A();
    virtual ~A();
    };

    class B : public A
    {
    public:
    B();
    private:
    virtual ~B();
    };



  3. #3
    Join Date
    Apr 1999
    Posts
    90

    Re: c++ question

    Also, if you declare the destructor as private, then no one can delete an object of class B. I believe even automatic variables as well as pointers.


  4. #4
    Join Date
    Apr 1999
    Posts
    3

    Re: c++ question

    Hi Michael,

    Thankyou for a reply but what I am unable to understand right now is that what OOPS concept is making it happen and why would one need the destructors to be private and Why don't they work when they are not virtual.

    Regards,
    Nitin Sahjwani


  5. #5
    Join Date
    Apr 1999
    Posts
    90

    Re: c++ question

    A vast majority of destructors are NOT private. Where are you getting the notion that you need a private destructor?

    When subclassing, the destructors are almost always virtual. This is to insure that all destructors in the chain get called.


  6. #6
    Join Date
    Apr 1999
    Posts
    3

    Re: c++ question

    Hi,

    The solution which you gave gives an error:

    B::~B' : cannot access private member declared in class 'B'

    Nitin


  7. #7
    Join Date
    Apr 1999
    Posts
    90

    Re: c++ question

    Destructors a generally not private. If you make it public you should be OK.
    I only made it private because that's what you originally had.


  8. #8
    Guest

    Re: c++ question

    If you make the destructor virtual, then all the destructors in the chain will NOT get called. It's the other way around, virtual says not to call the original one, non-virtual says to call all of them.


  9. #9
    Join Date
    Apr 1999
    Posts
    90

    Re: c++ question

    When it comes to the destructor, I disagree.
    Virtual destructors provide the means for each of the classes in the chain to clean themselves up in the reverse order that they were created. By not making them virtual, you run the risk of memory leaks because things weren't destroyed correctly.

    When you have other member functions that are virtual, then you're right you have to explicitly call the base class' function if that's what you want.



  10. #10
    Join Date
    May 1999
    Location
    Oregon, USA
    Posts
    302

    Re: c++ question

    I believe that Michael is correct.
    For an interesting discussion of this topic,
    see the June, 1998 C++ Q & A column in MSJ.



  11. #11

    Re: c++ question

    This is exactly correct. See Meyers, "Effective C++", Item 14.


  12. #12
    Guest

    Re: c++ question

    I had to test this to make sure and now I'm confused

    Here's the code I tried:

    class A
    {
    public:
    A();
    virtual ~A();
    };

    class B : public A
    {
    public:
    B();
    virtual ~B();
    };

    A::A()
    {
    printf("A::A()\r\n");
    }

    A::~A()
    {
    printf("A::~A()\r\n");
    }

    B::B()
    {
    printf("B::B()\r\n");
    }

    B::~B()
    {
    printf("B::~B()\r\n");
    }

    int main(int argc, char* argv[], char* envp[])
    {
    B b;
    return 0;
    }

    And if I declare the destructor as virtual, it behaves the same way as if I didn't. I have a feeling I missed something in the conversation here. Somebody wanna clear it up a bit? Thanks.


  13. #13
    Guest

    Re: c++ question

    I guess I should say what the output of the code in the previous post was:
    A::A()
    B::B()
    B::~B()
    A::~A()



  14. #14
    Join Date
    Apr 1999
    Posts
    90

    Re: c++ question

    That's the correct output for the example you're showing.

    Try it using pointers and dynamically creating the object using 'new'. That's generally how things are done anyway when you want polymorphism.

    For example:

    A *pObject = new B;
    delete pObject;

    Without virtual destructors, you might run into trouble with the above example.

    Let me know how you make out.

    Thanks,
    Michael


  15. #15
    Guest

    Re: c++ question

    Hi Michael,

    I guess I wasn't quite thinking If you delete an object using a pointer to the base class, you need virtual destructors to ensure that all of the destructors (even in derived class) get called....

    It all makes sense now

    Thanks...


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