这两个构造函数有什么区别?

发布于 2024-10-21 01:58:55 字数 296 浏览 1 评论 0原文

这两个构造函数有什么区别?

int x, y; //position 

BasePoint(int px, int py) : x(px), y(py) {} 

以及

int x, y; //position 

BasePoint(int px, int py)
{
    x = px;
    y = py;
}

x(px), y(py) 叫什么?什么时候使用这种类型的变量初始化?

谢谢。

What is the difference between this two constructor?

int x, y; //position 

BasePoint(int px, int py) : x(px), y(py) {} 

and

int x, y; //position 

BasePoint(int px, int py)
{
    x = px;
    y = py;
}

What is x(px), y(py) called? And When do I use this type of variable initialization?

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

手心的温暖 2024-10-28 01:58:55

第一个是使用initialization-list进行初始化,第二个是使用赋值运算符进行赋值

第一个推荐!

BasePoint(int px, int py) : x(px), y(py) {}
                          ^^^^^^^^^^^^^ this is called initialization-list!

阅读此常见问题解答:我的构造函数是否应该使用“初始化列表”或“作业”?

常见问题解答的开头是:

初始化列表。实际上,
构造函数应该初始化为
规则中的所有成员对象
初始化列表。一个例外是
下面进一步讨论[...]

阅读完整的答案。

First one is doing initialization using initialization-list, and second one is doing assignment using assignment operator.

First one is recommended!

BasePoint(int px, int py) : x(px), y(py) {}
                          ^^^^^^^^^^^^^ this is called initialization-list!

Read this FAQ : Should my constructors use "initialization lists" or "assignment"?

The FAQ answer starts with :

Initialization lists. In fact,
constructors should initialize as a
rule all member objects in the
initialization list. One exception is
discussed further down [...]

Read the complete answer.

扶醉桌前 2024-10-28 01:58:55

x(px), y(py) 叫什么?

这些称为初始值设定项列表。您实际上所做的是将 px 的值复制到 x 并将 py 复制到 y

用途:

class foo
{
    private:
    int numOne ;

    public:
    foo(int x):numOne(x){}
};

class bar : public foo
{
     private:
     int numTwo;

     public:
     bar(int numTwo): foo( numTwo ), // 1
                      numTwo(numTwo) // 2
     {}
 };

 bar obj(10);

1. 请注意,派生构造函数的参数可以传递给基类构造函数。

2. 在这种情况下,编译器可以解析哪一个是参数,哪一个是成员变量。如果,这需要在构造函数中完成,那么 -

 bar::bar(int numTwo) : foo( numTwo)
 {
     this->numTwo = numTwo; // `this` needs to be used. And the operation is called assignment. There is difference between initialization and assignment.
 }

What is x(px), y(py) called?

These are called initializer lists. What you are actually doing is copying the value of px to x and py to y.

Uses:

class foo
{
    private:
    int numOne ;

    public:
    foo(int x):numOne(x){}
};

class bar : public foo
{
     private:
     int numTwo;

     public:
     bar(int numTwo): foo( numTwo ), // 1
                      numTwo(numTwo) // 2
     {}
 };

 bar obj(10);

1. Notice that derived constructor's argument can be passed to base class constructor.

2. Compiler can resolve, in this case, which one is argument and which one is member variable. Had if, this needs to be done in the constructor,then -

 bar::bar(int numTwo) : foo( numTwo)
 {
     this->numTwo = numTwo; // `this` needs to be used. And the operation is called assignment. There is difference between initialization and assignment.
 }
玩套路吗 2024-10-28 01:58:55
BasePoint(int px, int py) : x(px), y(py) {} 

这里你正在使用初始化列表
因此,构造时的对象不会进入主体并初始化这些值。它通过不进入构造函数的主体来节省时间。

另一个用途是在调用派生类构造函数时。

如果你使用这样的语句

new derivedclass(a,b,c)

,你可以这样写

derivedclass(int x,int y,int z):baseclass(a,b),h(z){}
BasePoint(int px, int py) : x(px), y(py) {} 

here u are using the initialization list
so the object when constructed will not go inside the body and initiate those values.it saves time by not entering in the body of the constructor

Another use of this is when calling the derived class constructor.

where if you use the statement like

new derivedclass(a,b,c)

and you can write this

derivedclass(int x,int y,int z):baseclass(a,b),h(z){}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文