Java C++:使用抽象方法创建抽象类并覆盖子类中的方法
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2936844/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++: Create abstract class with abstract method and override the method in a subclass
提问by Martijn Courteaux
How to create in C++ an abstract class with some abstract methods that I want to override in a subclass? How should the .h
file look? Is there a .cpp
, if so how should it look?
如何在 C++ 中创建一个抽象类,其中包含一些我想在子类中覆盖的抽象方法?.h
文件应该怎么看?有没有.cpp
,如果有的话应该怎么看?
In Java it would look like this:
在 Java 中,它看起来像这样:
abstract class GameObject
{
public abstract void update();
public abstract void paint(Graphics g);
}
class Player extends GameObject
{
@Override
public void update()
{
// ...
}
@Override
public void paint(Graphics g)
{
// ...
}
}
// In my game loop:
List<GameObject> objects = new ArrayList<GameObject>();
for (int i = 0; i < objects.size(); i++)
{
objects.get(i).update();
}
for (int i = 0; i < objects.size(); i++)
{
objects.get(i).paint(g);
}
Translating this code to C++ is enough for me.
将这段代码翻译成 C++ 对我来说就足够了。
Edit:
编辑:
I created the code but when I try to iterate over the objects I get following error:
我创建了代码,但是当我尝试遍历对象时,出现以下错误:
Game.cpp:17: error: cannot allocate an object of abstract type ‘GameObject'
GameObject.h:13: note: because the following virtual functions are pure within ‘GameObject':
GameObject.h:18: note: virtual void GameObject::Update()
GameObject.h:19: note: virtual void GameObject::Render(SDL_Surface*)
Game.cpp:17: error: cannot allocate an object of abstract type ‘GameObject'
GameObject.h:13: note: since type ‘GameObject' has pure virtual functions
Game.cpp:17: error: cannot declare variable ‘go' to be of abstract type ‘GameObject'
GameObject.h:13: note: since type ‘GameObject' has pure virtual functions
With this code:
使用此代码:
vector<GameObject> gameObjects;
for (int i = 0; i < gameObjects.size(); i++) {
GameObject go = (GameObject) gameObjects.at(i);
go.Update();
}
采纳答案by Péter T?r?k
In Java, all methods are virtual
by default, unless you declare them final
. In C++ it's the other way around: you need to explicitly declare your methods virtual
. And to make them pure virtual, you need to "initialize" them to 0 :-) If you have a pure virtual method in your class, it automatically becomes abstract - there is no explicit keyword for it.
在 Java 中,所有方法都是virtual
默认的,除非你声明它们final
。在 C++ 中,情况正好相反:您需要显式声明您的方法virtual
。并且要使它们纯虚拟,您需要将它们“初始化”为 0 :-) 如果您的类中有纯虚拟方法,它会自动变为抽象的 - 没有明确的关键字。
In C++ you should (almost) always define the destructor for your base classes virtual
, to avoid tricky resource leaks. So I added that to the example below:
在 C++ 中,你应该(几乎)总是为你的基类定义析构函数virtual
,以避免棘手的资源泄漏。所以我将其添加到下面的示例中:
// GameObject.h
class GameObject
{
public:
virtual void update() = 0;
virtual void paint(Graphics g) = 0;
virtual ~GameObject() {}
}
// Player.h
#include "GameObject.h"
class Player: public GameObject
{
public:
void update();
void paint(Graphics g);
}
// Player.cpp
#include "Player.h"
void Player::update()
{
// ...
}
void Player::paint(Graphics g)
{
// ...
}
回答by James McNellis
The member functions need to be declared virtual
in the base class. In Java, member functions are virtual by default; they are not in C++.
成员函数需要virtual
在基类中声明。在 Java 中,成员函数默认是虚拟的;它们不在 C++ 中。
class GameObject
{
public:
virtual void update() = 0;
virtual void paint(Graphics g) = 0;
}
The virtual
makes a member function virtual; the = 0
makes a member function pure virtual. This class is also abstract because it has at least one virtual member function that has no concrete final overrider.
在virtual
使一个成员函数虚拟; 在= 0
使一个成员函数纯虚。这个类也是抽象的,因为它至少有一个没有具体的最终覆盖的虚成员函数。
Then in your derived class(es):
然后在您的派生类中:
class Player : public GameObject
{
public:
void update() { } // overrides void GameObject::update()
void paint(Graphics g) { } // overrides void GameObject::paint(Graphics)
}
If a member function is declared virtual in a base class, it is automatically virtual in any derived class (you can put virtual
in the declaration in the derived class if you'd like, but it's optional).
如果一个成员函数在基类中被声明为虚拟的,那么它在任何派生类中都会自动成为虚拟的(virtual
如果你愿意,你可以在派生类中加入声明,但它是可选的)。
回答by T.E.D.
In C++ you use the keyword virtual on your routines, and assign =0;
into them. Like so:
在 C++ 中,您在例程中使用关键字 virtual 并将其分配给=0;
它们。像这样:
class GameObject {
public:
virtual void update()=0;
virtual void paint(Graphics g)=0;
}
Having a virtual method with a 0
assigned into it automagically makes your class abstract.
拥有一个0
分配给它的虚拟方法会自动使您的类变得抽象。