Click here to Skip to main content
15,917,610 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
If in class A's constructor we create objects of other class B then do we need to destroy these class B objects in class A's destuctor?
Posted

It depends. Normally, what you create you must destroy. So, it depends on whether the B object is a member of A, or A holds a pointer to B as a member:

- If the B object is a member of class A, its constructor will be called in the process of A's construction and so its destructor will be called automatically from A's destructor. As the B object is a member, you haven't called new for it, so you don't need to do a delete.

- If class A contains a pointer to the B object (named m_pB for example), then you normally must destroy the B object in the destructor by something like

delete m_pB;


... except if the B object has already been destroyed in A's lifetime. Then of course, you don't need to destroy it a second time. It is good practice to either initialize m_pB with a NULL pointer in A's constructor or with an object pointer create with new. And when the B object is being destroyed prematurely, reflect that by setting m_pB to NULL. So, when A's destructor is called m_pB is either NULL or points to an object and in both cases you may legally call delete. In case of the NULL pointer, delete is simply a no-op.
 
Share this answer
 
You don't need to do anything in a class's destructor. That's just the way C++ works. However just because something's legal in the language doesn't mean you should do it.

Generally you should to do something in your destructors when:

- The object holds the last reference to a dynamically allocated object (you should delete it) whether it was allocated in an object's constructor or not [1].

- You object manages a resource (you should do whatever it takes to cleanly return the resource back where it came)

So if we rewrite your question a bit more specifically:

"If in class A's constructor we dynamically create objects of another class, B, then should we delete the objects of class B in class A's destructor?"

The answer is "Yes, unless the object of class A has passed ownership (a reference to the object of class B) to something else." [1]

The principle at work here is ownership - when an object A is responsible for managing the lifetime of object B "A owns B." This ownership can come from:

- B being a member of A's class [2]
- B being dynamically allocated by A
- A being given a reference to B

Oh, and be careful - it's hard to tell whether a class owns an instance of another one if your code uses pointers
C++
class A
{
    public:
        A();
        ~A();

       // Other members

    private:
        B *b_; // Do we own this, or are we just referencing it??
};
you can't tell whether ownership is occurring or not [3].

[1] Another, better, answer is "You never write delete. As soon as you dynamically create an object assign the pointer to the object to a pointer like object." Pointer like objects include unique_ptr, shared_ptr and vector - all means to reach object lifetime Nirvana.

Also you're a real mug if you use delete in your programs.

[2] This is an interesting one, 'cause if B is a member of A then A owns B and there's no way of giving ownership of B away - B is a part of A and you can't chop it out. You can't make a decision about when A gets rid of B - it just happens. When A goes to the bit-bucket in the sky then so does B.

[3] Which is where smart pointers come in... yay! As soon as you embrace smart pointers your code says more about it's intent. If you write your class like this:
C++
class A
{
    public:
        A();
        ~A();

    private:
        std::unique_ptr b_;
};
then you tell the reader than objects of class A own b_ as std::unique_ptr effectively converts a dynamically allocated object into an "ordinary" member. When objects of class A die, they take whatever b_'s pointing to with them.
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 11-Jun-12 0:19am    
A number of good points, my 5.
Perhaps, the question was about this: will destruction of A "automatically" destroy its contained objects of the class B. The answer: "of cause not".
--SA
Sandeep Mewara 11-Jun-12 2:31am    
Good points. 5!

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