Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. How to check object is valid.
Forum Updated to NodeBB v4.3 + New Features

How to check object is valid.

Scheduled Pinned Locked Moved Solved C++ Gurus
3 Posts 2 Posters 1.4k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • Taz742T Offline
    Taz742T Offline
    Taz742
    wrote on last edited by Taz742
    #1

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

    Do what you want.

    Taz742T jsulmJ 2 Replies Last reply
    0
    • Taz742T Taz742

      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();
      }
      
      
      Taz742T Offline
      Taz742T Offline
      Taz742
      wrote on last edited by
      #2
      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";
              }
      

      Do what you want.

      1 Reply Last reply
      0
      • Taz742T Taz742

        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();
        }
        
        
        jsulmJ Offline
        jsulmJ Offline
        jsulm
        Lifetime Qt Champion
        wrote on last edited by
        #3

        @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.

        https://forum.qt.io/topic/113070/qt-code-of-conduct

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved