title: Operator overloading for max in C++
body: <p>I am implementing a new variable type (NewType) with arithmetic operators (+, -, *, /) and std::max overloaded. Although the arithmetic operators work, the max function has got some problem. Could someone provide some pointers what I am missing?</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  class NewType {
  private:
      float val;
  
  public:
      NewType(float v) { val = v; }
      // Arithmetic operators
      friend NewType operator+(const NewType &amp;c1, const NewType &amp;c2);
      friend NewType operator-(const NewType &amp;c1, const NewType &amp;c2);
      friend NewType operator*(const NewType &amp;c1, const NewType &amp;c2);
      friend NewType operator/(const NewType &amp;c1, const NewType &amp;c2);
  
      float GetVal() { return val; }
  
      float max(const NewType &amp;lhs, const NewType &amp;rhs) { return lhs.val &gt; rhs.val ? lhs.val : rhs.val; }
      };
  
  // Arithmetic Operations
  NewType operator+(const NewType &amp;c1, const NewType &amp;c2) { return NewType(c1.val + c2.val); }
  NewType operator-(const NewType &amp;c1, const NewType &amp;c2) { return NewType(c1.val - c2.val); }
  NewType operator*(const NewType &amp;c1, const NewType &amp;c2) { return NewType(c1.val * c2.val); }
  NewType operator/(const NewType &amp;c1, const NewType &amp;c2) { return NewType(c1.val / c2.val); }
  
  
  int main() {
  
      NewType a = 10.2;
      NewType b = 8.4;
      NewType c = a+b;
  
      cout &lt;&lt; c.GetVal() &lt;&lt; std::endl;
  
      NewType d = max(a,b);
      cout &lt;&lt; d.GetVal() &lt;&lt; endl;
      return 0;
  }
  </code></pre>