How do I use a chain of operator overloads without modifying the operands?

允我心安 提交于 2019-12-01 04:52:06

You can simply use pass by value and do this:

A operator+ (A other) //pass by value
{
   other.a += a;
   return other;
}

Or, since the member a is publicly accessible, you can (rather should) make operator+ a non-member function as:

A operator+(A left, A const &right) 
{
   left.a += right.a;
   return left;
}

Notice that the first argument is accepted by value, and second by reference. In this way, you don't need to declare a local variable in the function. You can use the first parameter; after all it is local to the function, you can do whatever you want to do with it: in this case, we just add right.a to it, and return it.


A better design of class would be this: (read the comments)

class A
{
   int a;  //make it private
public:    
   A(int b) : a(b) //use member initialization list
   {
   }
   A& operator+=(A const & other)  //add `+=` overload, as member of the class
   {
      a += other.a;
      return *this;
   }
};

//and make `+` non-member and non-friend 
A operator+(A left, A const & right) 
{
  left += right; //compute this in terms of `+=` which is a member function
  return left;
}

There is no need to use pointers inside operator+. You can allocate the intermediate object in the stack and then return it:

A operator+ (const A &b)
{
   A c(a+b.a);
   return c;
}

Or just:

A operator+ (const A &b)
{
   return A(a+b.a);
}

Or even simpler:

A operator+ (const A &b)
{
   return a+b.a;
}

Since this implicitly calls A::A(int).

Note that I removed the reference from the return type. You can't return a non-const reference to a local.

Then you would use it this way:

A a(1),b(2),c(3),d;
d = a + b + c;

Note that d is no longer a reference.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!