To clarify something:
When your objects need to exist beyond the scope of a function, both the following declarations are wrong!
void myFunction()
{
MyClass1 nameOfTheObject;
MyClass2 *nameOfTheSecondObject = new MyClass;
}
void myFunction()
{
MyClass1 nameOfTheObject;
MyClass2 *nameOfTheSecondObject = new MyClass;
}
To copy to clipboard, switch view to plain text mode
While the memory of the second object will still exist after the scope of your function, it goes out of scope, meaning that you don't have any control over it anymore. To be 100% correct, you should delete the pointer before going out of scope.
To solve this, do this:
class MySuperClass
: public QObject{
...
private:
MyClass1 nameOfTheFirstObject;
MyClass2 *nameOfTheSecondObject;
};
class MySuperClass : public QObject
{
...
private:
MyClass1 nameOfTheFirstObject;
MyClass2 *nameOfTheSecondObject;
};
To copy to clipboard, switch view to plain text mode
Depending on how the pointer is managed, it needs to be deleted in the destructor of your class.
Bookmarks