C++:用抽象方法创建抽象类并覆盖子类中的方法 [英] C++: Create abstract class with abstract method and override the method in a subclass

查看:31
本文介绍了C++:用抽象方法创建抽象类并覆盖子类中的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 C++ 中创建一个抽象类,其中包含一些我想在子类中覆盖的抽象方法?.h 文件应该是什么样的?有没有.cpp,如果有的话应该怎么看?

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?

在 Java 中,它看起来像这样:

In Java it would look like this:

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);
}

将这段代码翻译成 C++ 对我来说就足够了.

Translating this code to C++ is enough for me.

我创建了代码,但是当我尝试遍历对象时,出现以下错误:

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

使用此代码:

vector<GameObject> gameObjects;

for (int i = 0; i < gameObjects.size(); i++) {
    GameObject go = (GameObject) gameObjects.at(i);
    go.Update();
}

推荐答案

在 Java 中,所有方法默认都是 virtual,除非你声明它们 final.在 C++ 中,情况正好相反:您需要显式声明您的方法 virtual.并且要使它们纯虚,您需要将它们初始化"为 0 :-) 如果您的类中有纯虚方法,它会自动变为抽象方法 - 没有明确的关键字.

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.

在 C++ 中,你应该(几乎)总是为你的基类 virtual 定义析构函数,以避免棘手的资源泄漏.所以我将其添加到下面的示例中:

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:

// 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)
{
     // ...
}

这篇关于C++:用抽象方法创建抽象类并覆盖子类中的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆