Surcharge opérateur comparaison et pointeurs
Bonjour,
Je tourne en rond depuis hier sur un problème de surcharge d'opérateur.
J'ai une classe mère abstraites objet
Code:
1 2 3 4 5 6 7 8 9
| class objet{
public:
virtual ~objet(){cout << "Objet detruit"<<endl;};
virtual void affiche(std::ostream& f=std::cout) const=0;
virtual objet* clone() const=0;
virtual bool operator=(int i){return false;}
virtual bool operator=(const string &s){return false;}[/INDENT]
}; |
et deux classes filles qui en hérite entier et chaine :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| class entier : public objet
{
private:
int nb;
public:
void affiche(std::ostream& f=std::cout) const;
entier* clone() const;
entier(int n) : nb(n){}
entier() : nb(0){}
int getEntier() const;
void setEntier(int n);
~entier();
bool operator=(int i){nb=i;return true;}
bool operator=(entier &n1){return true;}[/INDENT]
};
bool operator==(entier &n1,entier &n2);
bool operator==(chaine &n1,entier &n2);
bool operator==(entier &n1,chaine &n2);
ostream &operator << (ostream& os,entier &n1); |
et
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| class chaine : public objet
{
private:
string str;
public:
void affiche(std::ostream& f=std::cout) const;
chaine* clone() const;
chaine(string &ch) : str(ch){}
chaine() : str(""){}
chaine(const char*s) : str(s){}
string getChaine() const;
void setChaine(string ch);
~chaine();
bool operator=(const string &s){str=s;return true;}[/INDENT]
};
bool operator==(chaine &n1,chaine &n2);
ostream &operator << (ostream& os,chaine &ch); |
Ma question est la suivante :
si je declare deux pointeurs d'objet :
Code:
1 2
| objet *obj1=new entier(2);
objet *obj2=new chaine("toto"); |
comment puis je faire pour faire ceci :
qui me renvoie false si ce ne sont pas les mêmes classes ou si ce sont les mêmes classe mais pas les mêmes valeurs.
Merci pour vos futures réponses.