Reputation: 24061
Im new to this and just wanted to ask a quick question about deleting objects.
I have an object called MyClass1 and from it I have a number of other classes, MyClassA, MyClassB etc.
Now should I do this in MyClass1:
MyClass1::~MyClass1()
{
delete MyClassA;
delete MyClassB;
}
Or will everything created in MyClass1 automatically be deleted when I delete MyClass1?
Also, if I have more objects created in MyClassA and MyClassB, will these also have to be deleted manually in their respective class?
Thanks
Upvotes: 0
Views: 199
Reputation: 2554
delete
is applied to objects, not to classes. As a rule, calling delete
(or arranging to have it called automatically, via a shared pointer, or with the RAII idiom in general) is necessary only if you called new
to create the object. The exception is the return value of some (library?) call being an object that the (library's?) documentation states explicitly that the caller has to dispose of with delete
(in which case, think of the call as a wrapper around a new
that you have become responsible for.) Of course, APIs like that should be avoided if at all possible.
Upvotes: 0
Reputation: 7734
If Qt, use QPointer
! It is a smart pointer: nothing needed in destructor.
#include <QPointer>
class MyClass1
{
QPointer<MyClassA> pA;
QPointer<MyClassB> pB;
};
Upvotes: 0
Reputation: 96790
You can't delete objects that aren't pointers because that's not the purpose of delete
. It's meant to free dynamic memory associated with an object. That is, whatever is created with new
must be deleted. You can have pointers to a class, and they can be deleted. But since nothing was allocated with new
, there's no need to use delete
. The class will in fact be destructed from memory at the end of the scope in which it is created. Those objects are allocated on the stack while dynamic memory is on the heap. Objects on the stack have automatic storage duration (deleted at the end of its scope, unless its declared static
in which case it has "static" storage duration); moreover, objects on the heap have dynamic storage duration. Dynamic memory in C++ is controlled by you, that's why we are given new
and delete
(because C++ expects us to handle the memory ourselves). And otherwise deleting an object not constructed with new
is undefined behavior and may lead to a crash.
Upvotes: 1
Reputation: 10539
call delete
operator only if you have created your objects with new
operator
struct MyClass1
{
MyClassA mA;
MyClassB * mB;
MyClass1()
{
mB = new MyClassB;
}
~MyClass1()
{
delete mB;
}
};
Upvotes: 3
Reputation: 258558
If you're asking this, you're just learning C++, so the best advice is - neither. You should know about this stuff (dynamic allocation & memory management - see Guillaume's answer for this), but what you really should do is use RAII (google this). The proper C++ way of doing it would be:
struct MyClass1
{
MyClassA mA;
std::shared_ptr<MyClassB> mB;
MyClass1() : mB(new MyClassB)
{
}
};
See? No more destructor, which means you also don't need a copy constructor or copy assignment operator (which is where Guillaume's answer is flawed - it's missing the last two).
Upvotes: 4