d01 ex03 ref and ptr

This commit is contained in:
Hugo LAMY
2022-02-06 17:59:33 +01:00
parent 3708a135be
commit e165ba887d
7 changed files with 15 additions and 16 deletions

View File

@@ -1,19 +1,19 @@
#include "HumanA.hpp" #include "HumanA.hpp"
HumanA::HumanA( std::string name, Weapon weapon ) { HumanA::HumanA( std::string name, Weapon &weapon ) : _weapon( weapon ) {
this->_name = name; this->_name = name;
this->_weapon = &weapon; // this->_weapon = weapon;
return; return;
} }
HumanA::~HumanA() {return;} HumanA::~HumanA() {return;}
void HumanA::attack( void ) { void HumanA::attack( void ) const {
std::cout << this->_name << " attacks with their "; std::cout << this->_name << " attacks with their ";
std::cout << this->_weapon->getType() << std::endl; std::cout << this->_weapon.getType() << std::endl;
return; return;

View File

@@ -9,15 +9,15 @@ class HumanA {
public: public:
HumanA( std::string name, Weapon weapon ); HumanA( std::string name, Weapon &weapon );
~HumanA(); ~HumanA();
void attack( void ); void attack( void ) const;
private: private:
std::string _name; std::string _name;
Weapon * _weapon; Weapon & _weapon;
}; };

View File

@@ -7,7 +7,7 @@ HumanB::HumanB( std::string name ) {
} }
HumanB::~HumanB() {} HumanB::~HumanB() {}
void HumanB::setWeapon( Weapon weapon ) { void HumanB::setWeapon( Weapon &weapon ) {
this->_weapon = &weapon; this->_weapon = &weapon;
@@ -15,7 +15,8 @@ void HumanB::setWeapon( Weapon weapon ) {
void HumanB::attack( void ) { void HumanB::attack( void ) {
std::cout << this->_name << " attacks with their " << this->_weapon->getType() << std::endl; std::cout << this->_name << " attacks with their ";
std::cout << this->_weapon->getType() << std::endl;
} }

View File

@@ -13,7 +13,7 @@ public:
~HumanB(); ~HumanB();
void attack( void ); void attack( void );
void setWeapon( Weapon weapon ); void setWeapon( Weapon &weapon );
private: private:

View File

@@ -3,21 +3,19 @@
Weapon::Weapon( std::string type ) { Weapon::Weapon( std::string type ) {
this->_type = type; this->_type = type;
std::cout << "weapon constructor received : " << this->_type << std::endl; return;
} }
Weapon::~Weapon() {} Weapon::~Weapon() {}
std::string Weapon::getType( void ) { std::string Weapon::getType( void ) const {
return this->_type; return this->_type;
std::cout << "weapon getType() return : " << this->_type << std::endl;
} }
void Weapon::setType( std::string type ) { void Weapon::setType( std::string type ) {
this->_type = type; this->_type = type;
std::cout << "weapon setType() received : " << this->_type << std::endl;
} }

View File

@@ -11,7 +11,7 @@ public:
Weapon( std::string type ); Weapon( std::string type );
~Weapon(); ~Weapon();
std::string getType(); std::string getType() const;
void setType( std::string type ); void setType( std::string type );
private: private:

Binary file not shown.