Answer to Question #254819 in C++ for Rohit Swain

Question #254819

What is the difference between = operator overloading with returning object and without returning object.


1
Expert's answer
2021-10-22T00:49:00-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS