继承构造函数:从 C# 转换为 C++
我有 C# 背景,我会编写如下所示的类和构造函数:
public class Grunt : GameObject
{
public Grunt()
: base()
{
// do stuff
}
}
How do I write the constructorheritance in C++?在标题和源代码中。我知道您没有可以使用的“base”关键字,但是语法是否相同?
I'm coming from a C# background where I would write a class and constructor like this:
public class Grunt : GameObject
{
public Grunt()
: base()
{
// do stuff
}
}
How do I write the constructors inheritance in C++? In both the header and the source. I know you don't have the 'base' keyword to work with, but otherwise is the syntax the same?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
强烈考虑在 权威 C++ 书籍指南和列表中获取一本优秀的 C++ 书籍
And strongly consider getting one of the fine C++ books at The Definitive C++ Book Guide and List
是的,将
: base()
替换为: GameObject()
。但如果没有参数,则隐含调用,如 C# 中那样
Yes, replace
: base()
with: GameObject()
.But if there are no parameters the call is implied, as in C#
您可以使用基类的名称来代替“base”关键字。这是必要的,因为 C++ 中可能存在多重继承。如果有多个基类,您可以通过以逗号分隔调用来调用多个基构造函数。
You use the name of the base class in place of the 'base' keyword. This is necessary because of the possibility for multiple inheritance in C++. In the event of multiple base classes, you can call multiple base constructors by comma-delimiting the calls.
header:
source:
这阐明了如何从多个类继承,从模板类继承,构造基类,展示如何将参数传递给基构造函数,并展示为什么我们必须使用基类的名称。
header:
source:
This clarifies how to inherit from multiple classes, inherit from template classes, construct base classes, shows how to pass parameters to base constructors, and shows why we have to use the name of the base.