contructeur de copie et surcharge d opérateur
bonjour,
Comment puis je définir le constructeur de copie avec la surcharge d'opérateur et l'utilisation du pointeur this.
Code:
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
|
#include <iostream>
using namespace std;
class Compteur{
public:
Compteur();
Compteur(const Compteur&);
~Compteur(){}
int GetVal() const {return saVal;}
void setVal(int x){saVal = x;}
void Increment(){++saVal;}
const Compteur& operator++();
private:
int saVal;
};
Compteur::Compteur():saVal(0){};
const Compteur& Compteur::operator++(){
++saVal;
return *this;
}
Compteur::Compteur(const Compteur&){
cout << "Constructeur de copie !" << endl;
++*this;
}
int main()
{
Compteur i;
cout << "La valeur de i est " << i.GetVal() << endl;
i.Increment();
cout << "La valeur de i est " << i.GetVal() << endl;
++i;
cout << "La valeur de i est " << i.GetVal() << endl;
Compteur a = ++i;
cout << "La valeur de a " << a.GetVal();
cout << " et de i " << i.GetVal() << endl;
return 0;
} |
Au passage dans la constructeur de copie je me retrouve avec un comportement indéfini.
Merci