+ 1
How does operator-overloading work? Step by step
What are the components of operator-overloading? How does it work? What values does it calculate and return? I need someone who can answer me step by step the logic of this part of C++ - order of objects' definitions - time of operator 's execution - void constructor
23 ответов
+ 3
please read this for all the information and after that if you have any question then comment here. I am here to assist you.
https://www.programiz.com/cpp-programming/operator-overloading
+ 2
//sample code
#include <iostream>
using namespace std;
class Check
 { 
 private: 
    int i; 
 public:
    Check(): i(0) { }
    
    Check operator ++()
    { 
      Check temp; 
      ++i; 
      temp.i = i; 
      return temp;
    }
    
    void Display() 
    { 
      cout << "i=" << i << endl; 
    } 
 };
    
     int main() 
     { Check obj, obj1;
       obj.Display();
       ++obj;
       obj.Display(); 
       obj1 = ++obj;
       obj1.Display();
       return 0;
     }
+ 1
OrHy3 I did some change here for better understanding.
from prints you will get this-> var represents the object using which operator overloading method called and obj.var represents the object which we are passing.
please note the other change,
    MyClass res = obj1+obj2;
Internally compiler will call like below,
    MyClass res = obj1.operator+(obj2);
so now you can easily get way this->var has value of obj1 and obj.var has obj2 value.
we can also use internal code which compiler used to resolved operator overload function call.
#include <iostream>
using namespace std;
class MyClass {
    public:
        int var;
        MyClass() { }
        MyClass(int a)
        : var(a) { }
        MyClass operator+(MyClass &obj) {
            MyClass res;
            cout<<this->var<<endl;
            cout<<obj.var<<endl;
            res.var= this->var+obj.var;
            return res; 
        }
};
int main() {
    MyClass obj1(12), obj2(55);
    MyClass res = obj1.operator+(obj2);
    cout << res.var;
}
0
thanks
0
How can I use more than one object in an operation?
0
https://code.sololearn.com/cH179plEBuKq/?ref=app
0
https://code.sololearn.com/cQgU53Wm0kU2/?ref=app
I don 't understand why in the operator function this->var and obj.var don 't have the same value
0
OrHy3 try to run above code and you will get the idea.
0
So you 're saying to me that the operator function is executed by obj1 and use obj2 as its method, right?
0
You can more then two object for operation by using grouping via parentheses.
example:
#include <iostream>
using namespace std;
class MyClass {
    public:
        int var;
        MyClass() { }
        MyClass(int a)
        : var(a) { }
        MyClass operator+(MyClass &obj) {
            MyClass res;
            res.var= this->var+obj.var;
            return res; 
        }
};
int main() {
    MyClass obj1(12), obj2(55), obj3(100);
    MyClass res = (obj1 + obj2 )+ obj3;
    cout << res.var;
}
0
thanks too much
0
no, not method obj2 as argument.
 operator+ method is called for obj1 so this represents obj1. obj2 is passed in argument so obj.var represents obj2.
0
OrHy3 You are always wel come. Still if you have any questions then comment here. I will try my best to make it understandable.
0
ok, thanks
0
Just one question more: if there is more than one object, is the operator function called for the first object?
0
OrHy3 No not like that.
example:
(o1 + o2 ) + o3.
first parentheses should execute.
so ( o1 + o 2) , in this case operator method is called for o1 and o2 is passed as argument. returned object is stored in temporary object because + o3 is pending to execute.  
now the expression becomes,
temp + o3. so operator method is called for temp and o3 is passed into argument.
0
I thought in (o1 + o2) the function is called for o1, not for o2
0
https://code.sololearn.com/cQgU53Wm0kU2/?ref=app
Why does this code need a void constructor to work?
0
OrHy3 check line no 12 and 20,which needs default constructor.
if you don't want to do that try like below,
#include <iostream>
using namespace std;
class MyClass {
    public:
        int var;
      //  MyClass() { }
        MyClass(int a)
        : var(a) { }
        MyClass operator+(MyClass &obj) {
            MyClass res(0);
            res.var= this->var+obj.var;
            return res; 
        }
};
int main() {
    MyClass obj1(12), obj2(55);
    MyClass res (obj1+obj2);
    cout << res.var;
}



