I have my class object, if i have this object on the different places and when i deleted and initialized this object to NULL i want this object would be NULL on all the other places. It is possible?
#include "mainwindow.h"
#include <QApplication>
#include "QDebug"
class A {
public:
int x;
int y;
};
class B : public A {
public:
B(int a, int b) {
this->m = a;
this->n = b;
}
int m;
int n;
};
int main(int argc, char *argv[])
{
QApplication a(argc, argv);
B* temp = new B(1, 2);
B* b1 = temp;
B* b2 = temp;
delete temp;
temp = NULL;
qDebug() << b1->x << b2->x; //its print 421312312 -2131231231
return a.exec();
}
Thank @NathanOliver and all.
I am using QSharedPointer.
QSharedPointer<B> obj = QSharedPointer<B>(new B(1, 2));
obj.clear();
B* t = obj.data();
if (t)
qDebug() << t->m << t->n;
else {
qDebug() << "NULL";
}
now t is NULL. Thanks!
With std, it would be something like:
auto temp = std::make_shared<B>(1, 2);
std::weak_ptr<B> w1 = temp;
std::weak_ptr<B> w2 = temp;
temp.reset();
auto b1 = w1.lock();
auto b2 = w2.lock();
if (b1 && b2) {
qDebug() << b1->x << b2->x;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.