use*_*679 7 c++ syntax inheritance inline initializer-list
假设class Child
是类的派生类Parent
.在五个文件程序中,我将如何指定Child.h
我想要调用构造函数Parent
?我不认为以下内容在标题内是合法的:
Child(int Param, int ParamTwo) : Parent(Param);
Run Code Online (Sandbox Code Playgroud)
在这种情况下,Child.cpp
构造函数的语法应该是什么样的?
wol*_*k88 18
在Child.h中,您只需声明:
Child(int Param, int ParamTwo);
Run Code Online (Sandbox Code Playgroud)
在Child.cpp中,您将拥有:
Child::Child(int Param, int ParamTwo) : Parent(Param) {
//rest of constructor here
}
Run Code Online (Sandbox Code Playgroud)
构造函数的初始化列表是其定义的一部分.您可以在类声明中内联定义它
class Child : public Parent {
// ...
Child(int Param, int ParamTwo) : Parent(Param)
{ /* Note the body */ }
};
Run Code Online (Sandbox Code Playgroud)
或者只是宣布它
class Child : public Parent {
// ...
Child(int Param, int ParamTwo);
};
Run Code Online (Sandbox Code Playgroud)
并在编译单元中定义(Child.cpp
)
Child::Child(int Param, int ParamTwo) : Parent(Param) {
}
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
8488 次 |
最近记录: |