@op: Just out of curiosity: can you give us a use case for this? I'm afraid using STL or boost shared/unique pointers will cause more trouble than benefit. Using QObject parent/child relationship and one of the strong pointers on the same object instance you have two mechanisms controlling object life time that do not know of each other. This will sooner or later lead to crashes that are hard to track. As d_stranz already suggested you might better go with Qt smart pointers. However, QPointer might not be what you want. You may also consider QSharedPointer, which, by its documentation, seems to be a counterpart of std::shared_ptr which is aware of the special needs of Qt. Here's a small example showing the difference between QPointer and QSharedPointer. MyClass is a simple QObject derived class logging its creation and deletion.
#include "MyClass.h"
#include <iostream>
MyClass
::MyClass(QObject *parent
) :{
std::cout << "constructing MyClass object " << this << std::endl;
}
MyClass::~MyClass()
{
std::cout << "deleting MyClass object " << this << std::endl;
}
#include "MyClass.h"
#include <iostream>
MyClass::MyClass(QObject *parent) :
QObject(parent)
{
std::cout << "constructing MyClass object " << this << std::endl;
}
MyClass::~MyClass()
{
std::cout << "deleting MyClass object " << this << std::endl;
}
To copy to clipboard, switch view to plain text mode
The MainWindow CTOR looks like
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "MyClass.h"
#include <QPointer>
#include <QSharedPointer>
#include <iostream>
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
std::cout << "MainWindow::CTOR begin" << std::endl;
{
std::cout << "creating QPointer" << std::endl;
QPointer<MyClass> p = new MyClass(this);
std::cout << "creating QSharedPointer" << std::endl;
QSharedPointer<MyClass> sp(new MyClass(this));
}
std::cout << "MainWindow::CTOR end" << std::endl;
}
#include "MainWindow.h"
#include "ui_MainWindow.h"
#include "MyClass.h"
#include <QPointer>
#include <QSharedPointer>
#include <iostream>
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
std::cout << "MainWindow::CTOR begin" << std::endl;
{
std::cout << "creating QPointer" << std::endl;
QPointer<MyClass> p = new MyClass(this);
std::cout << "creating QSharedPointer" << std::endl;
QSharedPointer<MyClass> sp(new MyClass(this));
}
std::cout << "MainWindow::CTOR end" << std::endl;
}
To copy to clipboard, switch view to plain text mode
When running the program I get
MainWindow::CTOR begin
constructing MyClass object 0x13defe0
creating QSharedPointer
constructing MyClass object 0x110f780
deleting MyClass object 0x110f780
MainWindow::CTOR end
MainWindow::CTOR begin
creating QPointer
constructing MyClass object 0x13defe0
creating QSharedPointer
constructing MyClass object 0x110f780
deleting MyClass object 0x110f780
MainWindow::CTOR end
To copy to clipboard, switch view to plain text mode
The first MyClass object pointer 0x13defe0 is assigned to the QPointer instance, the second one (0x110f780) to QSharedPointer instance. Immediately after construction both pointers go out of scope. Only the MyClass object controlled by the QSharedPointer gets deleted.
Depending on your use case that might be a better choice.
Best regards
ars
Bookmarks