Share this post

Link to post

Share on other sites

"this" is a reference to the current object, basically meaning that if you are within the behavior of an object (a method), then "this" refers to that object. Example:

class A {private: int a;public: A(int a) {this->a = a; } };

Now, in this case, we have a variable shadow problem. The local variable passed into the constructor "A()" takes precedence over the classes variable, "a". So how do we access the classes variable? Simple, use the "this" reference, dereference the pointer, and use the variable (remember, within a class of the same type, you can call private variables).

So what other uses does this have? Well, maybe you want to register your class to another class by its reference? Maybe you are tracking objects and need to pass a classes reference for some reason from within it.

Share this post

Link to post

Share on other sites

this is a pointer to whatever class it is used in. If you have a class myclass, then this* points to a myclass, which happens to be the intance of myclass using the pointer. this pointer is used only inside the class itself. I have never explicitly had to use the this* pointer, but I have seen that it has uses, just none that I need yet. Also, anytime you refer to class members from inside the class, the compiler adds on the this* pointer automatically, though you don't see it.

Share this post

Link to post

Share on other sites

Original post by kburkhart84Also, anytime you refer to class members from inside the class, the compiler adds on the this* pointer automatically, though you don't see it.

Specifically, the compiler passes an extra parameter to a member function that it doesn't pass to a free function or to a static member function. This parameter is hidden, but still exists, that's why non-static member functions aren't compatible with free functions or static member functions (their signatures are essentially different), and why static member functions cannot directly access data members of a particular instance of a class; it lacks a this pointer - how does it know to what class a name is referring to - and you have to pass a reference or pointer to the static member function.

That's what this is for. For member functions, the compiler passes this for you. When member functions are static, you need to do this manually, ie., you need to pass *this (a reference to this object) or this (a pointer to this object) so you can work with data from that particular instance.

As well, if you ever need to return a pointer or reference to the object you're working with, you need to return this. There is no other way to refer to this object; you need this. Make sense?

Share this post

Link to post

Share on other sites

Original post by kburkhart84Also, anytime you refer to class members from inside the class, the compiler adds on the this* pointer automatically, though you don't see it.

Specifically, the compiler passes an extra parameter to a member function that it doesn't pass to a free function or to a static member function. This parameter is hidden, but still exists, that's why non-static member functions aren't compatible with free functions or static member functions (their signatures are essentially different), and why static member functions cannot directly access data members of a particular instance of a class; it lacks a this pointer - how does it know to what class a name is referring to - and you have to pass a reference or pointer to the static member function.

That's what this is for. For member functions, the compiler passes this for you. When member functions are static, you need to do this manually, ie., you need to pass *this (a reference to this object) or this (a pointer to this object) so you can work with data from that particular instance.

As well, if you ever need to return a pointer or reference to the object you're working with, you need to return this. There is no other way to refer to this object; you need this. Make sense?

Good point. Maybe to clarify as well, the this* pointer is used to say which "myclass" you are using and modifying. You only have to write the code once, but you can have many instances of "myclass" and this is how the compiler and you know which one you are talking about.PS: Forgive the stupid expanation, I wanted to put it in "english."