Implement assignment and copy constructor for battle_context.

This commit is contained in:
Rusty Russell 2006-04-10 03:28:49 +00:00
parent 91dbf319cc
commit 282342687f
2 changed files with 21 additions and 4 deletions

View file

@ -836,6 +836,23 @@ battle_context::battle_context(const gamemap& map, const std::vector<team>& team
}
}
battle_context::battle_context(const battle_context &other)
: attacker_stats_(NULL), defender_stats_(NULL)
{
*this = other;
}
battle_context& battle_context::operator=(const battle_context &other)
{
if (&other != this) {
delete attacker_stats_;
delete defender_stats_;
attacker_stats_ = new unit_stats(*other.attacker_stats_);
defender_stats_ = new unit_stats(*other.defender_stats_);
}
return *this;
}
battle_context::unit_stats::unit_stats(const unit &u, const gamemap::location& u_loc,
const attack_type *u_weapon, bool attacking,
const unit &opp, const gamemap::location& opp_loc,

View file

@ -148,8 +148,11 @@ public:
const gamemap::location& attacker_loc, const gamemap::location& defender_loc,
const attack_type& attacker_weapon);
battle_context(const battle_context &other);
~battle_context() { delete attacker_stats_; delete defender_stats_; }
battle_context& operator=(const battle_context &other);
// This method returns the statistics of the attacker.
const unit_stats& get_attacker_stats() { return *attacker_stats_; }
@ -157,9 +160,6 @@ public:
const unit_stats& get_defender_stats() { return *defender_stats_; }
private:
// Copy constructor and operator=() disabled. Don't copy battle_context, copy unit_stats instead.
battle_context(const battle_context &) {}
battle_context& operator=(const battle_context &) { return *this; }
// This method rates the defender weapon specified in 'd_stats', possibly taking into
// account the statistics of the attacker in the process ('a_stats'). The rating