Answers for "static operator overloading c++"

C++
1

c++ overload operator

#include <iostream>

class ExampleClass {
  public:
    ExampleClass() {}
  	ExampleClass(int ex) {
      example_ = 0;
    }
    int&       example()        { return example_; }
    const int& example() const  { return example_; }
  	//Overload the "+" Operator
  	ExampleClass operator+ (const ExampleClass& second_object_of_class) {
    	ExampleClass object_of_class;
    	object_of_class.example() = this -> example() + second_object_of_class.example();
    	return object_of_class;
  	}
  private:
  	int example_;
};

int main() {
  ExampleClass c1, c2;
  c1.example() = 1;
  c2.example() = 2;
  ExampleClass c3 = c1 + c2;
  //Calls operator+() of c1 with c2 as second_object_of_class
  //c3 gets set to object_of_class
  std::cout << c3.example();
}
Posted by: Guest on February-06-2021
0

operator = overloading c++

inline bool operator==(const X& lhs, const X& rhs){ /* do actual comparison */ }
inline bool operator!=(const X& lhs, const X& rhs){ return !(lhs == rhs); }
Posted by: Guest on April-18-2021
-1

operator overloading in c++

inline bool operator==(const X& lhs, const X& rhs){ return cmp(lhs,rhs) == 0; }
inline bool operator!=(const X& lhs, const X& rhs){ return cmp(lhs,rhs) != 0; }
inline bool operator< (const X& lhs, const X& rhs){ return cmp(lhs,rhs) <  0; }
inline bool operator> (const X& lhs, const X& rhs){ return cmp(lhs,rhs) >  0; }
inline bool operator<=(const X& lhs, const X& rhs){ return cmp(lhs,rhs) <= 0; }
inline bool operator>=(const X& lhs, const X& rhs){ return cmp(lhs,rhs) >= 0; }
Posted by: Guest on August-10-2021

Browse Popular Code Answers by Language