使用不带的子变量重写函数类
Use child variable without to rewrite function Class
我有一个问题,是否可以使用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(。第二种方法是,如果您希望能够创建一个项目容器,其中每个项目可以是Entity
或Player
(或者更准确地说,指向其中一个的指针(。然而,这也可以使用std::variant<Entity, Player>
来实现,并且这可能是优选的替代方案(特别地,每个项目仍将被"就地"分配,而不需要指向单独分配的项目的指针的容器(。
相关文章:
- 如何创建一个CMake变量,除非显式重写,否则使用默认值
- 将成员变量添加到共享库中的类中,不会破坏二进制兼容性吗
- 继承类中的 C++ 成员变量类型重写
- 重写子类构造函数中的父类变量
- 调用重写函数并使用基类中的重载变量
- 重写 const 函数以修改 C++ 中的类变量
- 为什么我的变量被重写了
- 重写用新关键字c++初始化的变量
- 可以在重写的方法中修改私有成员变量吗
- 重写基类的成员变量的初始值
- 如何实现类变量的虚拟函数重写
- c++重写已定义的变量
- 从父类继承时重写变量
- C++重写成员变量(std::vector)
- 访问被重写基类函数内部的成员变量
- 在Visual Studio中重写环境变量
- 重写派生类中的受保护变量
- OOP-这是重写类成员变量类型的好方法吗
- 在对象中创建另一个对象时,对象的成员变量将被重写
- 如何重写父类的静态变量