Niloufar Saeidi
Niloufar Saeidi

Reputation: 1

return object of operator= overloading in c++

What is the difference between returning *this or the given argument in implementation of operator= in C++? Is using one of them better or more useful? if yes, why?

   class Object {
   public:
      Object operator=(Object Obj) {
         return *this;
      }
   }

vs.

   class Object {
   public:
      Object operator=(Object Obj) {
         return Obj;
      }
   }

Upvotes: 0

Views: 68

Answers (1)

Yakk - Adam Nevraumont
Yakk - Adam Nevraumont

Reputation: 275896

X& operator=( X const& ) { return *this; } matches the semantics of = on an int. The other suggestions you gave do not. When in doubt match the semantics of int.

Upvotes: 1

Related Questions