(C++) operator+
January 8, 2018 · View on GitHub
(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); }