场景1:没有编译问题。当使用初始化列表在派生类中初始化基类时
class Base
{
public:
int x;
};
class D:public Base
{
public:
int y;
D(int y1):Base{y1+1},y{y1}{}
};
int main()
{
D d(5);
return 0;
}
方案2:不编译并要求使用参数化的构造函数。注意基类中的虚拟析构函数 $ g ++ -o main * .cpp main.cpp:在构造函数“ D :: D(int)”中: main.cpp:16:34:错误:没有匹配的函数可以调用“ Base :: Base()” D(int y1):基数{y1 + 1},y {y1} {} ^ main.cpp:5:7:注意:候选对象:Base :: Base() 班级基础 ^ ~~~ main.cpp:5:7:注意:候选人期望0个参数,提供1个 main.cpp:5:7:注意:候选者:constexpr Base :: Base(const Base&) main.cpp:5:7:注意:参数1没有从'int'到'const Base&'的已知转换
class Base
{
public:
int x;
virtual ~Base(){}
};
class D:public Base
{
public:
int y;
D(int y1):Base{y1+1},y{y1}{}
};
int main()
{
D d(5);
return 0;
}
答案 0 :(得分:1)
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<div class="panel-group" id="accordion" role="tablist" aria-multiselectable="false">
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingOne">
<h4 class="panel-title">
<a role="button" data-toggle="collapse" href="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
Collapsible Group Item #1
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in" role="tabpanel" aria-labelledby="headingOne">
<div class="panel-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingTwo">
<h4 class="panel-title">
<a class="collapsed" role="button" data-toggle="collapse" href="#collapseTwo" aria-expanded="false" aria-controls="collapseTwo">
Collapsible Group Item #2
</a>
</h4>
</div>
<div id="collapseTwo" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingTwo">
<div class="panel-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-heading" role="tab" id="headingThree">
<h4 class="panel-title">
<a class="collapsed" role="button" data-toggle="collapse" href="#collapseThree" aria-expanded="false" aria-controls="collapseThree">
Collapsible Group Item #3
</a>
</h4>
</div>
<div id="collapseThree" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingThree">
<div class="panel-body">
Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia
</div>
</div>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
是aggregate initialization。它仅适用于聚合。在您的第二个代码段中,由于Base{y1+1}
包含一个虚函数,因此它不再是聚合,因此您不能像这样初始化它。