"this" pointer help

Started by
7 comments, last by kburkhart84 18 years, 5 months ago
ive looked at previous "this" pointer topics for CplusPlus but
still havent figured out its purpose.
anybody have ideas?
For mankind so hated the world That it gave all it's begotten sons and daughters That whoever believed the lie To perish and receive everlasting hell
Advertisement
Would you explain any more in detail. Give us an example what you don't understand!

Thanks!
Julio A. Cruz
"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.
how does the compiler know what class and method this=> is referring to.
thank you!!!!
For mankind so hated the world That it gave all it's begotten sons and daughters That whoever believed the lie To perish and receive everlasting hell
Quote:Original post by seepauliedie
how does the compiler know what class and method this=> is referring to.


Context. Similar to how it works out which variables are in scope.

[size="1"]
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.


Quote:Original post by kburkhart84
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.

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?
:stylin: "Make games, not war.""...if you're doing this to learn then just study a modern C++ compiler's implementation." -snk_kid
Quote:Original post by seepauliedie
how does the compiler know what class and method this=> is referring to.
thank you!!!!

Essentially, this is a hidden argument that is passed to the function.

object.function(parameter);

is converted to
function(&object, parameter);


Because you always have to have an object to call the function, there is always an address to provide to that special this parameter.

CM
Quote:Original post by stylin
Quote:Original post by kburkhart84
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.

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."


This topic is closed to new replies.

Advertisement