c++中的构造函数初始化

Initialization by constructor in c++

本文关键字:初始化 构造函数 c++      更新时间:2023-10-16

我对构造函数有疑问,为什么下面的代码可以正常工作:

#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/Yqxvzk

可以这样初始化复杂类型的成员吗?

注。谢谢你的翻译和回答。抱歉语言错误

你想:

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