简体   繁体   English

C ++初始化构造函数中的未命名结构成员

[英]C++ initialized unnamed struct member in constructor

example: 例:

class A 
{
public:
    A();
private:
    struct {int x, y; } position_;
};

my question is: how to init position_.x and position_.y before the constructor function body? 我的问题是:如何在构造函数主体之前初始化position_.xposition_.y like: 喜欢:

A::A()     //init position_.x and position_.y here
{
}

Just use a normal constructor initializer list: 只需使用普通的构造函数初始化器列表即可:

A::A()
    : position_{0, 0}
{
}

You can initialize it like any data member 1 : in the constructor initialization list: 您可以像构造任何数据成员1一样初始化它:在构造函数初始化列表中:

A() : position_{1,2} {}    

at the point of declaration: 在声明时:

struct {int x, y;} position_{1, 2};

or both. 或两者。


1 Assuming you have C++11 or higher 1假设您具有C ++ 11或更高版本

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

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