Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Inheritance C+中的继承构造函数安全性+;_Inheritance_C++11_Constructor_Default Constructor - Fatal编程技术网

Inheritance C+中的继承构造函数安全性+;

Inheritance C+中的继承构造函数安全性+;,inheritance,c++11,constructor,default-constructor,Inheritance,C++11,Constructor,Default Constructor,我有这样的层次结构: struct Params { int k = 17; }; struct A { A(Params& par): _p(par) { } Params& _p; }; struct B: public A { using A::A; int c{this->_p.k}; }; int main() { Params p; B b(p); return 0; } 我可以保证在声明B

我有这样的层次结构:

struct Params { int k = 17; };
struct A
{
    A(Params& par): _p(par)
    { }

    Params& _p;
};

struct B: public A
{
    using A::A;
    int c{this->_p.k};
};

int main()
{
    Params p;
    B b(p);
    return 0;
}
我可以保证在声明
B::c
调用之前,
A::p
将始终进行初始化吗


谢谢

是,它将被初始化

在开始构造派生类之前,必须完全构造基类。因此,只要
A
中的构造函数初始化
A::\p
,派生类将始终*看到它完全初始化

[*]当然,只要付出足够的努力,你总能打破一切;例如:
在那里,我使用逗号操作符调用
B::foo()
,然后才能构造任何东西;幸运的是,编译器将其检测为警告。

是的,
a::\p
来自基类,因此必须在派生类开始构造之前对其进行初始化。谢谢!你应该把这个作为答案!