*this vs this in C++

Hello,
I'm confused. I don't know what is differences between this and *this.

If we use this in the below program we have an error.

Thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
 #include <iostream>
#include <string>

using namespace std;

class myClass
{
private:

    int x, y;

public:

    myClass() {}

    myClass(int i, int j)
    {
        x = i;
        y = j;
    }

    myClass operator>=(myClass p2);

    void show()
    {
        cout << x << '\t' << y;
    }


};


myClass myClass::operator>=(myClass p2)
{
    myClass temp(0, 0);

    if ((x + y) > (p2.x + p2.y))
        return *this;
    else
        return temp;
}

int main()
{
    myClass a(11, 2), b(10, 2), c;

    c = a >= b;

    c.show();




    cout << "\n\n";
    system("pause");
}
Last edited on
Do you understand the concept of pointers and what dereferencing a pointer will do/allow?

this is a pointer to a C++ object.

https://www.tutorialspoint.com/cplusplus/cpp_this_pointer.htm

If line 38 were written as return this; what would be returned from the member function? what is being returned now?
Last edited on
>= is a relational operator, your member function should return a bool.

https://en.cppreference.com/w/cpp/language/operators
"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.
Last edited on
teaching pointers is not good for new students.

It makes for quite a few headaches for early-on self-taught noobs as well. I know that from experience.

Pointers is one of the easiest-to-mess-up concepts in C/C++. IMO.
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 :)
Last edited on
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++." :)
Last edited on
Topic archived. No new replies allowed.