latley我花了很多的Java编程。在这里你调用继承自 super(); (你可能都知道)的类
在C ++中有一个类,它有一个默认的构造函数,它接受一些参数。示例:
class BaseClass { public: BaseClass(char * name); ....如果我继承该类,它会给我警告,没有适当的默认构造函数。所以在C ++中有一些类似于 super()的东西,还是我需要定义一个函数来初始化所有的变量?
<$ c $ c> class Foo:public BaseClass { public: Foo():BaseClass(asdf){} };接受参数的基类构造函数必须在初始化成员之前调用。
latley I spent much programming in Java. There you call the class you Inherited from with super(); (you all probably know that)
Now I have a class in C++ which has a default constructor which takes some arguments. Example:
class BaseClass { public: BaseClass(char *name); ....If I inherit the class it gives me the warning, that there is no appropriate default constructor available. So is there something like super() in C++, or do I have to define a function where I initialize all variables?
解决方案You do this in the initializer-list of the constructor of the subclass.
class Foo : public BaseClass { public: Foo() : BaseClass("asdf") {} };Base-class constructors that take arguments have to be called there before any members are initialized.
更多推荐
如何调用基类构造函数?
发布评论