answersLogoWhite

0


Best Answer

The this pointer is a constant pointer which stores the address of the current instance of Myclass. There are three values associated with the this pointer:

&this : the address of the thispointer

this : the address of the current instance of Myclass

*this : the dereferenced value of the current instance of Myclass

Since *this is the dereferenced value of this, when a function needs to return a reference it will automatically return this value by reference.

Thus for a Myclass class with the following definition:

class Myclass {

private:

int attribute;

public:

Myclass& operator= (const Myclass&);

};

The implementation of the assignment operator would be:

Myclass& Myclass::operator= (const Myclass& rhs)

{

attribute = rhs.attribute;

return *this;

}

The implementation might be clearer if you implement it this way:

Myclass& Myclass::operator= (const Myclass& rhs)

{

Myclass& result = *this; // directly refer to this instance.

result.attribute = rhs.attribute;

return result;

}

In the above example, *this (the dereferenced value of this) is assigned to the reference named result. Since result is a reference (not a variable), resultrefers to the same instance pointed to by this.

Thus,

result.attribute = rhs.attribute;

is exactly the same as if we'd said

this->attribute = rhs.attribute;

// or, attribute = rhs.attribute;

and

return result;

is the same as if we'd said

return *this;

Note that since we already have a pointer (this), we do not need a reference as well. We gain nothing by using a direct reference in this way. Of course it would have made more sense had the this pointer been implemented as a reference rather than a pointer, however C++ evolved from C and C has no concept of a distinct reference type. Thus the earliest implementations of C++ used pointers just like C. References were added much later and to switch to references now would require a fundamental change to C++ that would literally break billions of lines of code, especially every single return *this; statement.

User Avatar

Wiki User

9y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: Please tell me in C how does return this relates to returning a reference in assignment operator overloading I am confused because this is a pointer and Myclass is a reference.?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

What is Jonas reaction to his assignment after the chief elder names the qualities he possesses?

Jonas is initially shocked and confused by his assignment as the Receiver of Memory. He feels honored and special to be chosen, but also anxious and apprehensive about the responsibilities and challenges that come with the role. Ultimately, he accepts the assignment with a sense of duty and determination to fulfill his role in the community.


Myth of Quetzalcoatl?

The god was believe to have left and would return one day in the year 1 atl. Cortes confused Montezume and made him believe he was the returning god, Quetzalcoatl


How do you use the word enfold in a sentence?

As the volcano erupted, he watched the lava flow enfold his house and incinerate it. The returning soldier sprinted from the plane to enfold his fiancee in his arms. (*not to be confused with the similar word 'unfold' which can mean to occur)


Did Jonas agree or disagree with the receiver of memory assignment?

Jonas ultimately disagreed with the assignment as he realized the Receiver's role involved far more pain and suffering than he had initially been led to believe. He rebelled against the oppressive nature of the community and chose to leave in search of a better life.


How can you use the word clarification in a sentence?

The diagram was complicated and needed clarification.I need further clarification in order to understand this bookJust remember that "clarification" is the noun form of the verb "clarify."


What is a fedex reference number?

A FedEx reference number is a number provided by the sender to track a package. It can be a combination of numbers and letters that helps identify a specific shipment. This number is different from the tracking number assigned by FedEx.


What is 7 is 6 times as much as 42?

A very confused statement.A very confused statement.A very confused statement.A very confused statement.


Past tense of confused?

The past tense of "confused" is "confused." For example: "He was still confused about the directions even after asking for help."


Can you give a sentence using confused?

I am confused by your question. She confused the students with her talk of parabolas. Don't become confused, go straight on and get your goal.


Where was Sir Tristan from?

some debate on this one. the pictish king lists have a Drustan which many celticists believe is the origin, the change to the T was when fin amour dudes in France were confused by the name and thought it must be a reference to sadness.


What are frogs confused with?

Frogs are often confused with toads.


How do you say I am confused in Spanish?

I am confused is Estoy confundido