派生 class 继承 getter,无法找到 return 正确值的方法

Derived class inherited getter, can't find a way to return the correct value

我试图让派生的 class(英雄)从基础 class(实体)继承 getter 函数的代码。但是,我找不到如何通过这个 getter 访问 Hero 的私有变量(正确的值)。

我计划将类似的 getter(总共大约 10 个)分配给英雄 class 以及另一个派生的 class(敌人)。虽然我可以在技术上为两个 class 写出每个 getter,但我宁愿限制代码重复。无论如何我可以在实体中编写代码并让两个派生的 classes 继承它吗?

#include <iostream>
using namespace std;

class Entity{
public:
    Entity() {
       this->speed = 0;
    }
    short getSpeed() {
       return this->speed;
    }

private:
    string name;
    short speed;
};

class Hero : public Entity{
public:
    Hero(short speed) {
       this->speed = speed;
    }

private:
    short speed;
};

int main()
{
    Hero hero1(2);
    cout << hero1.getSpeed() << endl;
    return 0;
}

输出给我0,这是entity的默认值。有没有办法访问2hero1值并输出它?

你需要在英雄中实现 getSpeed class 因为速度是一个私有变量

这是您更正后的代码:

#include <iostream>
using namespace std;

class Entity{
public:
    Entity() {
       this->speed = 0;
    }
    short getSpeed() {
       return this->speed;
    }

private:
    string name;
    short speed;
};

class Hero : public Entity{
public:
    Hero(short speed) {
       this->speed = speed;
    }

    short getSpeed() {
       return this->speed;
    }
private:
    short speed;
};

int main()
{
    Hero hero1(2);
    cout << hero1.getSpeed() << endl;
    return 0;
}

可能最好改用 protected

#include <iostream>
using namespace std;

class Entity{
public:
    Entity() {
       this->speed = 0;
    }
    short getSpeed() {
       return this->speed;
    }

protected:
    string name;
    short speed;
};

class Hero : public Entity{
public:
    Hero(short speed) {
       this->speed = speed;
    }
};

int main()
{
    Hero hero1(2);
    cout << hero1.getSpeed() << endl;
    return 0;
}

为什么您需要基 return 值的方法,该值对派生而言是私有的?那不是你通常做的事情。

退一步想想你真正想要达到的目标。如果每个 Enitity 都有一个 speed 成员,并且如果每个 Hero 都是一个 Entity,那么 Hero 不需要额外的私有 speed

相反,Hero 应该在构造函数中初始化它的 Entity 部分:

class Entity{
public:
    Entity(short speed = 0) : speed(speed) {}   // <- fixed constructor
    short getSpeed() { return speed; }
    void setSpeed(short s) { speed = s; }
private:
    short speed;
};

class Hero : public Entity{
public:
    Hero(short speed) : Entity(speed) {}    
};

我更改了 Entity 的构造函数,以便您可以为 speed 传递初始值。然后 Hero 的构造函数可以正确地初始化它的 Entity 子对象。

Is there any way to access the hero1 value of 2 and output it?

如果您真的希望 speed 成为 Hero 的私有成员,那么您也应该在 Hero 中实现 getter,就像您为 Entity。但是,在 classes 和 getter 中都有一个 speed 有点奇怪。选择 speed 属于 Entity 还是属于 Hero,你不太可能同时需要它。

写代码之前你应该回答的一个问题是:谁负责什么?

在上面的示例中,Entity 负责管理其速度。我们可以这样说来扭转局面:Entity 只需要一种检索速度的方法。这实际上是如何完成的是 subclasses 的业务(想想木椅 vs 精灵弓箭手穿着速度 +5 的靴子)。在代码中

struct Entity{
    virtual short getSpeed() { return 0; }
};

我无法比其他人解释得更好,所以我引用 cppreference:

Virtual functions are member functions whose behavior can be overridden in derived classes. As opposed to non-virtual functions, the overridden behavior is preserved even if there is no compile-time information about the actual type of the class. If a derived class is handled using pointer or reference to the base class, a call to an overridden virtual function would invoke the behavior defined in the derived class. [...]

TL;DR: virtual 使用指针和引用启用动态调度。它鼓励 subclasses 用自己的实现覆盖该方法。

现在 subclasses 可以使用默认实现(木椅)或提供自己的:

struct ElvenArcher : Entity {
    bool hasBootsOfSpeed = true;
    short baseSpeed = 10;
    short getSpeed() override {
        return hasBootsOfSpeed ? (baseSpeed+5) : baseSpeed;
    }
};

此处override 声明该方法重写基class 中的一个。

PS:注意我把重要的部分放在了粗体中。从你的问题中不清楚什么是编写代码的正确方法,这个答案主要来自评论太长的评论。我试图概述两个极端。您实际需要的可能介于两者之间。