Go back to Richel Bilderbeek's homepage.

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

 

 

 

 

 

(C++) operator/

 

operator/ is the operator for dividing.

 

The following line of code calls operator/ to divide the values of two integers:

 

const int x = 12 / 4;

 

operator/ is encapsulated by the functor std::divides.

 

 

 

 

 

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(12);
  const Test t2(4);
  const Test t3 = t1 / t2;
  assert(t3.mX == 3);
}

 

 

 

 

 

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

Go back to Richel Bilderbeek's homepage.

 

Valid XHTML 1.0 Strict