3
QObject *obj; ... if ( /* obj is already instantiated */ ) { ; } else { obj = new QObject(); } 

My query is the condition of the if

3 Answers 3

5

1) Initialize your object pointer to NULL

2) Check for NULL in your if statement

QObject *obj = NULL; ... if ( obj != NULL ) { ; } else { obj = new QObject(); } 
Sign up to request clarification or add additional context in comments.

3 Comments

Actually, NULL is a C-ism. For a modern compiler, please use nullptr instead.
I guess Qt discarded NULL. And ask to use 0 in stead. But ISO forbids initialization of members like error I get when using QObject *obj = 0;
If obj is a member variable, then it should be initialized to 0/NULL/nullptr (take your pick) inside of your class's constructor, preferably in the initializer list.
0
QObject *obj = 0; // ... if (!obj) obj = new QObject(); 

Note: Makes no guarantee obj is not a dangling pointer.

Comments

0

Use QPointer class. http://qt-project.org/doc/qt-4.8/qpointer.html#data

Whenever you call deleteLater(); it will set it to 0, than you can check it.

QPointer<QWidget> someWidget = 0;

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.