I have the following code to overload operator+, which works fine when the program gets executed.
In the main() function, when I re-write the statement to call the overloaded operator+ from res= t + t1 + t2, which works fine, to res = t + (t1 + t2), it does not work anymore. Can anyone provide me a solution, along with the reason?
A solution already found is to update the signatures of the operator+ from Test operator +(Test &a) to Test operator +(const Test &a). Here, I have used the const keyword in the parameter list.
#include <iostream>
using namespace std;
class Test
{
private:
    int num;
public:
    Test(int v)
    {
        num = v;
    }
    Test operator+(Test &a)
    {
        Test r(0);
        r = num + a.num;
        return r;
    }
    void show()
    {
        cout << "\n num = " << num;
    }
};
int main()
{
    Test t(10);
    Test t1(20);
    Test t2(60);
    Test res(0);
    res = t + t1 + t2;
    res.show();
    return 0;
}
 
     
    