I am a student of computer science. Please read my question fully before answering
In C++ class today we learned about overloaded operators, in particular the assignment operator, and my professor said something he doesn't like to: "Just trust me." He uttered this line in reference to the return *this
convention.
I asked, "But why?" and his answer was pretty much, "Because it is."
I am not satisfied with this answer.
Consider the following:
class length
{
private:
int inches, feet, yards;
public:
//getters, setters, etc.
Length operator=(const Length& Q)
{
this->inches = Q.inches;
this->feet = Q.feet;
this->yards = Q.yards;
return *this;
}
};
I understand based on mucking about through countless questions and at least 3 C++ books that the "Convention" exists because it allows chained assignments but why is it necessary and how did it come to be?
A more in-depth question is how does this
have individual "child pointers" (my term, not official) to the properties of the class (IE this->inches
)? How does that work? Is this->inches
just an offset or something?
My professor and I would REALLY appreciate an answer that isn't just "Because it's how it's done."
Please and Thank You
edit: I thought I was clear in writing the question; however, based on the responses I have been getting, I believe there has been a miscommunication. I am seeking to understand where the convention came from (roots in C I believe) and why it is this way.