如何调用基类构造函数?

编程入门 行业动态 更新时间:2024-10-22 16:37:07
本文介绍了如何调用基类构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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.

更多推荐

如何调用基类构造函数?

本文发布于:2023-10-31 07:36:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545453.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:函数

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!