How to check object is valid.



  • Hello.
    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();
    }
    
    


  • 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";
            }
    

  • Moderators

    @Taz742 said in How to check object is valid.:

    qDebug() << b1->x << b2->x; //its print 421312312 -2131231231

    Yes, it prints garbage because nobody initialises x.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.