简体   繁体   English

在 C++ 中通过构造函数初始化

[英]Initialization by constructor in c++

I have question about constructor, why following code works correctly:我对构造函数有疑问,为什么以下代码可以正常工作:

#include <iostream>
using namespace std;
class mycl
{
private:
    int a;
    //struct
    //{
        char b,c;
    //} ms;

public:
    mycl (int _a,char _b,char _c):a (_a), b (_b), c (_c){}

};

int main() {
    // your code goes here
    mycl slc (15, 'a', 'f');
    return 0;
}

https://ideone.com/wBgM1b https://ideone.com/wBgM1b

but there is a compilation error in this one但是这个有编译错误

https://ideone.com/Yqxvzk https://ideone.com/Yqxvzk

is it possible to initialize members of complex types this way?是否可以通过这种方式初始化复杂类型的成员?

ps thank for translate and for answer. ps 感谢翻译和回答。 sorry about wrong language对错误的语言感到抱歉

You want:你要:

mycl(int _a, char _b, char _c) : a(_a), ms{_b, _c} {}
//                                      ^^^^^^^^^^

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

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