Forum |  HardWare.fr | News | Articles | PC | S'identifier | S'inscrire | Shop Recherche
2118 connectés 

  FORUM HardWare.fr
  Programmation
  C++

  operator+= vs operator+

 


 Mot :   Pseudo :  
 
Bas de page
Auteur Sujet :

operator+= vs operator+

n°2067284
in_your_ph​ion
Posté le 31-03-2011 à 14:16:06  profilanswer
 

bonjour,
j'aimerais savoir s'il y a une difference avec les operateur "+" et "+=" quand on additionne deux string par exemple ?
 

Code :
  1. string a("aa" ); string b("bb" );
  2. a = a+b;
  3. a+=b;


 
?  
 
est ce que l'un est plus rapide que l'autre ? y'a t'il une différence?
merci
 
ps:j'ai une autre question, pour le code suivant "friendly string operator" :
 

Code :
  1. #include <iostream>
  2. #include <string.h>
  3. using namespace std;
  4. class String{
  5. public:
  6.     String();
  7.     String(const char *const);
  8.     String(const String & );
  9.     ~String();
  10.     char & operator[](int offset);
  11.     char operator[](int offset) const;
  12.     String operator+(const String& );
  13.     friend String operator+(const String&, const String& );
  14.     void operator+=(const String& );
  15.     String & operator= (const String & );
  16.     int GetLen()const { return itsLen; }
  17.     const char * GetString() const { return itsString; }
  18. private:
  19.     String (int);
  20.     char * itsString;
  21.     unsigned short itsLen;
  22. };
  23. String::String()
  24. {
  25.     itsString = new char[1];
  26.     itsString[0] = '\0';
  27.     itsLen=0;
  28. }
  29. String::String(int len)
  30. {
  31.     itsString = new char[len+1];
  32.     for (int i = 0; i<=len; i++)
  33.         itsString[i] = '\0';
  34.     itsLen=len;
  35. }
  36. String::String(const char * const cString)
  37. {
  38.     itsLen = strlen(cString);
  39.     itsString = new char[itsLen+1];
  40.     for (int i = 0; i<itsLen; i++)
  41.         itsString[i] = cString[i];
  42.     itsString[itsLen]='\0';
  43. }
  44. String::String (const String & rhs)
  45. {
  46.     itsLen=rhs.GetLen();
  47.     itsString = new char[itsLen+1];
  48.     for (int i = 0; i<itsLen;i++)
  49.         itsString[i] = rhs[i];
  50.     itsString[itsLen] = '\0';
  51. }
  52. String::~String ()
  53. {
  54.     delete [] itsString;
  55.     itsLen = 0;
  56. }
  57. String& String::operator=(const String & rhs)
  58. {
  59.     if (this == &rhs)
  60.         return *this;
  61.     delete [] itsString;
  62.     itsLen=rhs.GetLen();
  63.     itsString = new char[itsLen+1];
  64.     for (int i = 0; i<itsLen;i++)
  65.         itsString[i] = rhs[i];
  66.     itsString[itsLen] = '\0';
  67.     return *this;
  68. }
  69. char & String::operator[](int offset)
  70. {
  71.     if (offset > itsLen)
  72.         return itsString[itsLen-1];
  73.     else
  74.         return itsString[offset];
  75. }
  76. char String::operator[](int offset) const
  77. {
  78.     if (offset > itsLen)
  79.         return itsString[itsLen-1];
  80.     else
  81.         return itsString[offset];
  82. }
  83. String String::operator+(const String& rhs)
  84. {
  85.     int  totalLen = itsLen + rhs.GetLen();
  86.     String temp(totalLen);
  87.     int i, j;
  88.     for (i = 0; i<itsLen; i++)
  89.         temp[i] = itsString[i];
  90.     for (j = 0, i = itsLen; j<rhs.GetLen(); j++, i++)
  91.         temp[i] = rhs[j];
  92.     temp[totalLen]='\0';
  93.     return temp;
  94. }
  95. String operator+(const String& lhs, const String& rhs)
  96. {
  97.     int  totalLen = lhs.GetLen() + rhs.GetLen();
  98.     String temp(totalLen);
  99.     int i, j;
  100.     for (i = 0; i<lhs.GetLen(); i++)
  101.         temp[i] = lhs[i];
  102.     for (j = 0, i = lhs.GetLen(); j<rhs.GetLen(); j++, i++)
  103.         temp[i] = rhs[j];
  104.     temp[totalLen]='\0';
  105.     return temp;
  106. }
  107. int main()
  108. {
  109.     String s1("String One " );
  110.     String s2("String Two " );
  111.     char *c1 = { "C-String One " } ;
  112.     String s3;
  113.     String s4;
  114.     String s5;
  115.     cout << "s1: " << s1.GetString() << endl;
  116.     cout << "s2: " << s2.GetString() << endl;
  117.     cout << "c1: " << c1 << endl;
  118.     s3 = s1 + s2;
  119.     cout << "s3: " << s3.GetString() << endl;
  120.     s4 = s1 + c1;
  121.     cout << "s4: " << s4.GetString() << endl;
  122.     s5 = c1 + s2;
  123.     cout << "s5: " << s5.GetString() << endl;
  124.     return 0;
  125. }


Je ne comprend pas pourquoi  s4 = s1 + c1; appel operator+ avec un seul argument, alors que s5 = c1 + s2; appelle celui avec deux argument, car dans les deux cas il y a une conversion implicite de char* en string.
 
merci...

Message cité 1 fois
Message édité par in_your_phion le 31-03-2011 à 14:27:48
mood
Publicité
Posté le 31-03-2011 à 14:16:06  profilanswer
 

n°2067319
Joel F
Real men use unique_ptr
Posté le 31-03-2011 à 16:13:26  profilanswer
 

in_your_phion a écrit :


est ce que l'un est plus rapide que l'autre ? y'a t'il une différence?
merci


 
 a += b evite une copie inutile.
 

in_your_phion a écrit :


Je ne comprend pas pourquoi  s4 = s1 + c1; appel operator+ avec un seul argument, alors que s5 = c1 + s2; appelle celui avec deux argument, car dans les deux cas il y a une conversion implicite de char* en string.
merci...


 
s1 + c1 c'est string::operator+(char*)
c1 + s2 c'est operator+(char*,string)
 
En general les gens civilisés ecrivent operator+(X,Y) non friend en appelant +=
 

Code :
  1. class Foo
  2. {
  3.   public:
  4.   operator+=(Foo const& )
  5.   {
  6.       // some code
  7.   }
  8. };
  9. operator+(Foo const& a, Foo const& b )
  10. {
  11.    Foo tmp(a);
  12.    tmp += b;
  13.   return tmp;
  14. }


 
pas de friend, pas de copie ;)

n°2067324
in_your_ph​ion
Posté le 31-03-2011 à 16:24:24  profilanswer
 

hello,
 
merci! mais je ne comprend pas tout :(
 

Joel F a écrit :


 a += b evite une copie inutile.


 
où est ce que la copie ce fait quand on fait a = a + b ?
 
car pour moi cela équivaut à a = a.operator+(b), donc juste des affectations
 

Joel F a écrit :


s1 + c1 c'est string::operator+(char*)
c1 + s2 c'est operator+(char*,string)

Code :
  1. class Foo
  2. {
  3.   public:
  4.   operator+=(Foo const& )
  5.   {
  6.       // some code
  7.   }
  8. };
  9. operator+(Foo const& a, Foo const& b )
  10. {
  11.    Foo tmp(a);
  12.    tmp += b;
  13.   return tmp;
  14. }


 
pas de friend, pas de copie ;)


 
on n'est pas obligé de mettre de type de retour ?
 
haa... la il y a une copie ok! :) mais dans le cas où ce n'est pas friendly, il n'y a jamais de copie non ?  
 
merci

n°2067491
in_your_ph​ion
Posté le 01-04-2011 à 11:10:34  profilanswer
 

hello & up

 
Joel F a écrit :


 a += b evite une copie inutile.

 

est ce que ça veut dire que pour a = a + b on a :
- creation d'un buffer temporaire a1, où l'on stoque a + b (addition)
- creation d'un second buffer temoraire a2 où l'on copie a+b (égal)

 

alors que pour a+=b, on a :
- creation d'un buffer temporaire a2, où l'on stoque a + b (addition et egal)

 

c'est ça ?

 
Joel F a écrit :


s1 + c1 c'est string::operator+(char*)
c1 + s2 c'est operator+(char*,string)

 

par contre la je comprend toujours pas ... :(


Message édité par in_your_phion le 01-04-2011 à 11:10:53
n°2067552
mr simon
Posté le 01-04-2011 à 14:26:50  profilanswer
 

Dans "a = a + b", il y a d'abord un appel a "string operator+(const string& )" qui retourne donc un nouvel objet. Ensuite il y a appel a l'operateur d'affectation (operator=) pour copier cet objet temporaire dans a.
 
Alors que "a += b", il y a un appel a operator+= qui se contente d'ajouter au buffer de a le contenu de b.
 
Enfin, dans s1 + c1, vu que s1 est avant c1, le compile appel string::operator+(char*) (et vice versa pour c1 + s2)

n°2067824
in_your_ph​ion
Posté le 04-04-2011 à 10:12:44  profilanswer
 

mr simon a écrit :

Enfin, dans s1 + c1, vu que s1 est avant c1, le compile appel string::operator+(char*) (et vice versa pour c1 + s2)

 

hello,
merci pour ta réponse, je crois que j'ai compris pour le 1.

 

Par contre le 2. pour moi c'est :

 

s1 + c1, qui équivaut à s1.operator+( String(c1) ) ....ok là je comprend

 

c1 + s2 .... le je pige plus .... car pour moi ce serait donc c1 qui est convertit en String, donc ensuite, c'est juste c1.operator+(String& ) qui serait appelé, puisque que c1 est déjà une String et non plus une C-String ....

 

:cry:

 

help me ...


Message édité par in_your_phion le 04-04-2011 à 10:13:15

Aller à :
Ajouter une réponse
  FORUM HardWare.fr
  Programmation
  C++

  operator+= vs operator+

 

Sujets relatifs
friend operatorchainage d'appel surcharge operator
syntaxe : operator T&() const;[Résolu]pb compilation operator ++
operator&operator << (std::ostream& O, const A& a)
operator = et constT_OBJECT_OPERATOR
Conflits avec les operator >> et <<std::sort et operator d'affectation
Plus de sujets relatifs à : operator+= vs operator+


Copyright © 1997-2022 Hardware.fr SARL (Signaler un contenu illicite / Données personnelles) / Groupe LDLC / Shop HFR