使用不带的子变量重写函数类

Use child variable without to rewrite function Class

本文关键字:变量 重写 函数      更新时间:2024-09-27

我有一个问题,是否可以使用children变量来显示某些内容。

例如:

// Entity.hpp
class Entity {
public:
Entity();
~Entity();
// For this function
virtual void attack();
virtual int getNbAttack();
protected:
private:
int _nbAttack = 0;
int _nbAttackCost = 0;
std::string _msgAttack = "message class Entity";
};
// Player.hpp
class Player : public {
public:
Entity();
~Entity();
protected:
private:
int _nbAttack = 5;
int _nbAttackCost = 5;
std::string _msgAttack = "message class Player";
};
// Entity.cpp

int Entity::attack() {
this->_power -= 10;
std::cout << _msgAttack << std::endl;
return this->_nbAttack;
}

有可能这样做吗:

// Main.cpp
Entity entity;
entity.attack();
std::cout << entity.getNbAttack() << std::endl;
Player player;
player.attack();
std::cout << player.getNbAttack() << std::endl;

结果:

message class Entity
0
message class Player
5

这可能吗?还是我必须重写我的函数?

提前感谢您的帮助!

你绝对可以做到。但你需要对你发布的代码进行一些更改:

  • Entity作为基类添加到Player的类定义中(它在public之后丢失(
  • 使Entity的数据成员受保护或为其添加受保护的setter
  • 如果要在派生类中使用基类的构造函数,请使用using Entity::Entity;,但要分别用Player();~Player();声明Player的构造函数/析构函数
  • 缺少成员变量声明(power(
  • [可选]您可以去掉this->,以便从类内部访问成员。你可以使用它,但不是绝对必要的

一般来说,我有两个提示:

第一:试试看。使用编译器并运行程序。您还可以使用以下页面https://godbolt.org/运行您的代码(注意,对于初学者来说,这可能是太多的信息(。

第二:读一本关于编程语言的好书和/或查看https://en.cppreference.com/w/cpp

你可以这样做,但至少在我看来,这是一个相当糟糕的主意。

如果你想要两个在其他方面相似但存储不同固定值的类,我会使用一个模板:

#include <iostream>
// This is to represent a string literal, since we can't actually
// pass a string literal as a template argument.
template<size_t N>
struct string_literal {
char data[N];
constexpr string_literal(const char (&input)[N]) {
std::copy_n(input, N, data);
}
operator char const *() const { return data; }
};
template <int N, string_literal name>
class basic_entity {
public:
int attack() const {
std::cout << name << " attack ";
return nbAttack;
}
private:
int nbAttack = N;
};
int main() {
using Entity = basic_entity<0, "Entity">;
using Player = basic_entity<5, "PLayer">;
Entity entity;
Player player;
std::cout << entity.attack() << "n";
std::cout << player.attack() << "n"; 
}

这会产生这样的结果:

Entity attack 0
PLayer attack 5

我可以看到两个值得考虑继承的案例。第一种情况是使用较旧的编译器(string_literal类需要C++20(。第二种方法是,如果您希望能够创建一个项目容器,其中每个项目可以是EntityPlayer(或者更准确地说,指向其中一个的指针(。然而,这也可以使用std::variant<Entity, Player>来实现,并且这可能是优选的替代方案(特别地,每个项目仍将被"就地"分配,而不需要指向单独分配的项目的指针的容器(。