Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Meaning of (QWidget *parent = 0) in constructor MyClass(QWidget *parent = 0);

just a part of a usual structured class in Qt:

class MyClass  :  public QWidget           
{

Q_OBJECT

 public:

   MyClass(QWidget *parent = 0);
.
.
.
}

Looking at the constructor I don't understand the meaning of the parameter (QWidget *parent = 0) ? What does this mean?

greetings

like image 824
Streight Avatar asked May 14 '12 21:05

Streight


2 Answers

MyClass(QWidget *parent = 0) defines a constructor that can take a QWidget*.

You might be confused at the = 0 part, which is C++ syntax for default arguments. Default arguments allow you to use the function without having to specify that particular argument. In the case of this, you could call this constructor like this:

mc = MyClass();

And this is equivalent to calling:

mc = MyClass(0); // or MyClass(NULL)

And that means that MyClass object will have no parent QWidget, because = 0 means the parent is a null pointer.

like image 111
逆さま Avatar answered Oct 05 '22 06:10

逆さま


When you construct a new MyClass, you give it a pointer to a QWidget that you want to be the parent of the new one. The = 0 means that if you do not supply an argument, it will have no parent. Or, more strictly speaking, its parent pointer will be set to NULL.

like image 36
Almo Avatar answered Oct 05 '22 06:10

Almo