A question about OOP
-
What's the difference between 2 below implementations? Case 1:
class animal{ animal& operator=(animal& ani){ //few operations return *this; } }
Case 2:class animal{ animal operator=(animal& ani){ //few operations return *this; } }
Please note the reference sign (&) in return type of overloading operator. Which implementation is correct? Best regards. -
What's the difference between 2 below implementations? Case 1:
class animal{ animal& operator=(animal& ani){ //few operations return *this; } }
Case 2:class animal{ animal operator=(animal& ani){ //few operations return *this; } }
Please note the reference sign (&) in return type of overloading operator. Which implementation is correct? Best regards.Case 1 is correct, because it returns a reference instead of a copy of this object. See also: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccelng/htm/overl_14.asp[^]
-
What's the difference between 2 below implementations? Case 1:
class animal{ animal& operator=(animal& ani){ //few operations return *this; } }
Case 2:class animal{ animal operator=(animal& ani){ //few operations return *this; } }
Please note the reference sign (&) in return type of overloading operator. Which implementation is correct? Best regards.when you return a reference, you will be able tou chain the operators like this:
animal MyAnimal1, MyAnimal2, MyAnimal3; // Declarations
MyAnimal1 = animal(/*accepted constructor parameters*/); // here, both of the operator=() could work
MyAnimal3 = MyAnimal2 = MyAnimal1; // here, it must be a reference returnedin general, we prefer the synopsis
**T& operator= (const T&)**
... make your choice:)
TOXCCT >>> GEII power