Skip to content

Latest commit

 

History

History
66 lines (37 loc) · 1.53 KB

CppOperatorDivide.md

File metadata and controls

66 lines (37 loc) · 1.53 KB

 

 

 

 

 

 

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); }