Go back to Richel Bilderbeek's homepage.

Go back to Richel Bilderbeek's C++ page.

 

 

 

 

 

(C++) operator+

 

operator+ is the operator for adding.

 

The following line of code calls operator+ to add the values of two integers:

 

const int x = 3 + 4;

 

operator+ is encapsulated by the functor std::plus.

 

 

 

 

 

Example function to overload operator+

 

#include <cassert>
 
struct Test
{
  Test(const int x = 0) : mX(x) {}
  int mX;
};
 
const Test operator+(const Test& lhs, const Test& rhs)
{
  return Test( lhs.mX + rhs.mX );
}
 
int main()
{
  const Test t1(3);
  const Test t2(4);
  const Test t3 = t1 + t2;
  assert(t3.mX == 7);
}

 

 

 

 

 

Go back to Richel Bilderbeek's C++ page.

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict