fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct test {
  5. int x;
  6.  
  7. explicit test(int x = 0) : x(x) {}
  8.  
  9. test operator + (test second) {
  10. return test(x + second.x);
  11. }
  12.  
  13. test operator + (int second) {
  14. return test(x + second);
  15. }
  16. };
  17.  
  18. int main() {
  19. test a(20), b(22), c;
  20.  
  21. c = a + b; // call first overload
  22. cout << c.x << endl;
  23.  
  24. c = a + 22;
  25. cout << c.x << endl;
  26. }
Success #stdin #stdout 0s 3296KB
stdin
Standard input is empty
stdout
42
42