"this" is a pointer to the object you're currently working on. *this is dereferencing that pointer - which is simply getting the value stored at the memory address.
If we use this in the below program we have an error.
What is the exact error message?
Something like this?
In member function 'myClass myClass::operator>=(myClass)':
38:16: error: could not convert '(myClass*)this' from 'myClass*' to 'myClass'
41:1: warning: control reaches end of non-void function [-Wreturn-type]
This error states that this has type myClass*, a pointer.
You have set the function to return a value that has type myClass, a class.
The compiler does not know how to implicitly dereference a pointer to get the value pointed to by the pointer. You have to explicitly dereference the pointer.
The purpose of error messages is to give you information. With that you (must learn to) focus on the (syntactic) problems.
myClass myClass::operator>=(myClass p2)
What is the logical function of this operator?
Usually the >= means greater or equal and returns true, if the left operand is greater or equal to the right operand.
Standarn Library has algorithm std::max that returns the largest value that it gets as arguments.
Your operator ... returns either the value of left operand or value of myClass(0, 0). What is the logic of that design?
this (pun?) is why saying that teaching pointers is not good for new students.
try to avoid this. the most common need for 'this' is using the same parameter twice, eg if you have a class member named foo and the parameter to the setter for it is also called foo then you end up with this->foo = foo;
if you simply change the parameter names, its more normal:
foo = incoming;
that won't clear up all the uses of this pointer, but it gets rid of pointless ones.
there is nothing you can do about needing it when returning a reference to the current object.
agreed, but they need enough exposure to it to understand the * and -> etc syntax, because of the above.
I apparently stopped mid thought above, I meant to say, the issue here is why I think at least a basic exposure to pointers (without dynamic memory parts) is critical. Of course, he could have been napping that day, maybe they do cover it. And I am not picking on the OP -- I miss my share of things :)
If'n new-ish students are going to deal with memory management via pointers, teach 'em how to USE smart pointers first. IMO manual memory management is not a beginner's subject
First learn to use. Then as an intermediate/advanced course learn old school memory management techniques.
Since lots of older code is littered with it.
Same goes for C++ containers/strings vs. C arrays.
Sadly teaching "modern" C++ via C++11/14/17 seems to be too hard of a change. For both the instructors and the curriculum.
</rant off>
I seem to experiencing some deja vu about "how to teach the basics of C++." :)