What is the difference between = operator overloading with returning object and without returning object.
#include <iostream>
using namespace std;
class Example
{
public:
Example() {};
Example(int m)
{
member = m;
}
// operator overloading with returning object
Example& operator=(const Example &ex1)
{
member = ex1.member;
return *this;
}
// operator overloading without returning object
void operator=(const int m)
{
this->member = m;
}
int member{ 0 };
};
int main()
{
// When the = operator is reloaded with a returning object, the result of the function
// execution is a reference to the class object
// Sample code
Example t(5);
Example t1;
t1 = t;
cout << t1.member<<endl;
// When you reload the = operator without returning an object, the function performs
//some operations but does not return a value or a reference to a class member
// Sample code
t1 = 10;
cout << t1.member<<endl;
return 0;
}
Comments
Leave a comment