考虑以下类。 (来自游戏,但大大简化了。)
战斗.h:
class Combat {
public:
Combat();
Combat(int health, int offense, int defense);
virtual ~Combat();
int attack();
int defend();
int health() const;
void setHealth(int health);
private:
struct CombatImpl;
std::unique_ptr<CombatImpl> _impl;
};
战斗.cc:
struct Combat::CombatImpl {
CombatImpl();
CombatImpl(int health, int offense, int defense);
~CombatImpl()=default;
int _health;
int _offense;
int _defense;
};
Combat::Combat(int health, int offense, int defense) :
_impl { new Combat::CombatImpl(health, offense, defense) } {
}
Combat::~Combat()=default;
int Combat::attack() {
int hits = 0;
for(int i = 0; i < _impl->_offense; i++ ) {
if (rand() % 6 == 5) {
hits++;
}
}
return hits;
}
int Combat::defend() {
int parries = 0;
for(int i = 0; i < _impl->_defense; i++ ) {
if (rand() % 6 == 5) {
parries++;
}
}
return parries;
}
int Combat::health() const {
return _impl->_health;
}
void Combat::setHealth(int health) {
_impl->_health += health;
}
Combat::CombatImpl::CombatImpl(int health, int offense, int defense) {
_health = health;
_offense = offense;
_defense = defense;
}
怪物.h:
class Monster: public Combat {
public:
Monster(int health, int offense, int defense);
virtual ~Monster();
}
怪物.cc:
Monster::Monster(int health, int offense, int defense)
: Combat(health, offense, defense) {}
Monster::~Monster()=default;
播放器.h:
class Player : public Combat {
public:
Player();
virtual ~Player();
private:
struct PlayerImpl;
static PlayerImpl _impl;
};
播放器.cc:
struct Player::PlayerImpl {
PlayerImpl()=default;
~PlayerImpl()=default;
} Player::_impl;
Player::Player() : Combat(17, 1, 1) {
}
Player::~Player()=default;
...最后,一个使用它们的测试程序:
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <memory>
using namespace std;
#include "monster.h"
#include "player.h"
static Monster monster(3, 1, 1);
void fight() {
Player player;
int damage = monster.attack();
damage -= player.defend();
if ( damage > 0 ) {
player.setHealth(-damage);
}
if ( player.health() < 1 ) {
return;
}
damage = player.attack();
damage -= monster.defend();
if ( damage > 0 ) {
monster.setHealth(-damage);
}
if ( monster.health() < 1 ) {
return;
}
}
int main() {
Player player;
srand(time(NULL));
while (player.health() > 0 && monster.health() > 0) {
fight();
printf("player health = %d monster health = %d\n", player.health(),
monster.health());
}
}
如果你运行这个程序,你会发现它不起作用。怪物的生命值应该减少,但玩家的生命值停留在其初始值。我认为它正在发生的原因是这样的; Player 只有静态数据(封装在 PlayerImpl _impl 中)这样我就可以拥有一个全局 Player 对象,我可以从代码中的不同函数调用它。 (单态模式。)但是它的基类 Combat 是动态的。所以发生的事情是每次我创建 Player player;在 fight() 中,我实际上得到了一个新的 Combat,其中 Combat::_health 是默认值。当玩家超出范围时,对 _health 的任何更改都会丢失。在 Monster 中,这不是问题,因为 Monster 对象也有动态数据。理想情况下,我可以说
class Player : public static Combat {
意思是只在这个特定的战斗中使用 make static 但这是一个语法错误。还有另一种方法吗?还是我把自己逼到了墙角?
最佳答案
听起来您还没有真正考虑过封装层次结构。从战斗中派生的玩家没有多大意义,您的实现困惑(和这个问题)支持了这一点。您已经与 C++ 为我们提供多重继承而不是接口(interface)这一事实发生冲突,因为我相信您要描述的是 Player 具有 Combat 接口(interface)。
我们解决此类问题的一种常见方法是使用转发器/桥接器/委托(delegate)器/特征/访问器类,在这种情况下可能是“Combatant”或“CombatHandler”或“CombatEntity”——一切都取决于您希望继承的方式阅读 - 其唯一目的是帮助您遍历封装图;在这种情况下,从一个实体到封装该类实体的战斗功能。
这些中间类并不意味着简单,仅限于互连逻辑。不要在其中放置任何实际功能;尽量让所有成员保持常量。
class Combatant {
public:
Combatant() {}
virtual const Combat* Combat() const = 0; // so combat is technically our impl
virtual Combat* Combat() = 0;
// keep this interface light, it's primarily an accessor interface.
virtual bool CanFight() const { return (Combat() != nullptr); }
virtual bool CanFight(Combatant* opponent_) const {
return (opponent_ != nullptr && CanFight() && opponent_->CanFight());
}
};
class PassiveEntity() : Combatant {
...
const Combat* Combat() const { return nullptr; }
Combat* Comat() { return nullptr; }
}
class Player : public Combatant {
public:
virtual const Combat* Combat() const override {
// if you HAVE to use a static, something like this.
return &s_playerCombatImpl;
}
virtual Combat* Combat() override {
// but really it should be a member so it can be stateful.
return &m_combat;
}
...
};
class Monster : public Combatant {
...
};
class Corpse : public PassiveEntity {
...
};
您应该重构的第二件事是导致您调用不带参数的全局函数而不是调用
monster.fight(player);
//or
player.fight(monster);
我怀疑这是因为您正在尝试实现框架但尚未封装它,因此框架不知道参与者是谁,而您通过使用全局变量来强制执行它。
再看看您的原作,并回顾一下静态的使用如何迫使您的手更进一步:将战斗细节和意识提升到 Player 类中,进一步打破您的封装。
这并不是说必须不惜一切代价避免单例或全局变量,只是确保您自己检查一下——您的意思是说此信息对任何类都是可见的和可修改的,包括“PrawnShriveller”和“MP3Player”,以及全局函数“WhenIdleFormatHardDriveCatchFireOrDoOtherThings()”?
关于c++ - 静态构造函数初始化器可能吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16875862/