如何调用基类构造函数

如何调用基类构造函数

本文介绍了如何调用基类构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

这篇关于如何调用基类构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 11:18