-6

I've been studying operator overloading and i cant understand this, whenever i use s1=+s2 answer is s1=1 s2=1 and when i use s2=+s1 i get s1=2 s2=2 please explain

#include<iostream>
using namespace std;
class score
{   private:
    int val;
    public:
    score()
    {   val=0;  }
    score operator+()
    {   score temp;
        val=val+1;
        temp.val=val;
        return temp;    }
    void show()
    {   cout<<val<<endl;    }
};
main()
{
    score s1,s2;
    s1.show();
    s2.show();
    +s1;
    s1=+s2;
    s1.show();
    s2.show();
}
Akhilesh Sharma
  • 133
  • 1
  • 1
  • 9

1 Answers1

0
 s2=+s1

Means merely nothing more than applying the unary operator+() to the rvalue provided with the operator=()'s parameter.

That boils down to the same statement as

s2 = (+s1);

There's no combined operator like operator+=() for that.

That would have no effect at all, unless these operator functions are overloaded for specific (non primitive) types.

πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190