简体   繁体   English

构造函数MyClass(QWidget * parent = 0)中(QWidget * parent = 0)的含义;

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

just a part of a usual structured class in Qt: 只是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) ? 看一下构造函数我不明白参数的含义(QWidget *parent = 0) What does this mean? 这是什么意思?

greetings 问候

MyClass(QWidget *parent = 0) defines a constructor that can take a QWidget* . MyClass(QWidget *parent = 0)定义了一个可以接受QWidget*的构造函数。

You might be confused at the = 0 part, which is C++ syntax for default arguments . 您可能会对= 0部分感到困惑,这是默认参数的 C ++语法。 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. 这意味着MyClass对象将没有父QWidget ,因为= 0表示父对象是空指针。

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. 当你构造一个新的MyClass时,你给它一个指向你想成为新的MyWlass的父级的QWidget的指针。 The = 0 means that if you do not supply an argument, it will have no parent. = 0表示如果您不提供参数,则它将没有父级。 Or, more strictly speaking, its parent pointer will be set to NULL. 或者,更严格地说,其父指针将设置为NULL。

First of all its good to use explicit keyword following constructor function like 首先,使用显式关键字后面的构造函数就好了

explicit MyClass(QWidget *parent = 0 );

By using explicit ,it means you will not be able to do an implicit assignment. 通过使用explicit,这意味着您将无法执行隐式赋值。

For better understanding lets take an example 为了更好地理解,我们举个例子

Suppose you create a class MyInteger that takes an integer in the constructor. 假设您创建了一个在构造函数中采用整数的类MyInteger。 Then you can write something like this: 然后你可以这样写:

MyInteger integer = 5;

For simple classes, this can work ok, but when you have complex classes, implicit assignments can result in errors. 对于简单类,这可以正常工作,但是当您有复杂的类时,隐式赋值可能会导致错误。 Therefor it is best to have explicit assignments: 因此,最好有明确的作业:

MyInteger integer = MyInteger(5);

This way you are sure that everything is correct. 这样你就可以确保一切正确。 It's a safeguard. 这是一种保障。

What about the statement "QWidget *parent = 0"! 语句“QWidget * parent = 0”怎么样!

What is that for? 那是做什么用的?

You define a constructor with a default value. 您使用默认值定义构造函数。 The default value in this case means that there is no parent. 在这种情况下,默认值意味着没有父级。 Assigning 0 to a pointer (in this case) 将0分配给指针(在本例中)

MyClass(QWidget *parent = 0)

means that you create a pointer to nothing. 表示您创建一个指向任何内容的指针。 In any normal case, you want a pointer to point to a certain address in memory, like the address of a parent widget. 在任何正常情况下,您都希望指针指向内存中的某个地址,例如父窗口小部件的地址。 But when you don't want to set a parent widget for example, you can point to nothing. 但是,当您不想设置父窗口小部件时,您可以指向任何内容。 Be aware though when using the pointer, always check that it actually points to something (and preferably something correct) or don't do anything with it. 请注意,虽然在使用指针时,始终检查它是否实际指向了某些东西(最好是正确的东西)或者不对它做任何事情。

That's a default parameter . 这是一个默认参数 If no parameter is supplied, the one provided is used. 如果未提供参数,则使用提供的参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM