1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51
| #include <toto.h>
#include <cstring>
char * Toto::mystrdup(char const *str)
{
char * ret = new char[std::strlen(str) + 1];
std::strcpy(ret, str);
return ret;
}
//Constructeur
Toto::Toto(void)
{
nom = mystrdup("Toto");
nbYeux = 2;
nbBras = 2;
nbJambes = 2;
nbCheveux = 0;
}
//Constructeur par copie
Toto::Toto(Toto const & instance)
{
nom = mystrdup(instance.nom);
nbYeux = instance.nbYeux;
nbBras = instance.nbBras;
nbJambes = instance.nbJambes;
nbCheveux = instances.nbCheveux;
}
//Destructeur
Toto::~Toto(void)
{
delete[] nom;
}
//Opérateur d'affectation: Utilise l'idiome copy-and-swap
Toto & Toto::operator= (Toto tmp)
{
tmp.Swap(*this);
return *this;
}
void Toto::Swap(Toto & instance)
{
std::swap(nom, instance.nom); //On échange juste les pointeurs
std::swap(nbYeux, instance.nbYeux);
std::swap(nbBras instance.nbBras);
std::swap(nbJambes, instance.nbJambes);
std::swap(nbCheveux, instance.nbCheveux);
} |