"this" pointer

Started by
8 comments, last by smart_idiot 19 years, 2 months ago
I've recently been coding in pure C for my games and I am going back to using C++ classes in my programs. My question is, what is the purpose of the "this" pointer? I looked it up in my C++ book, but it dosen't really explain it's usefulness, just that it can be used to access members from within a method or something like that. Thanks!
-----------------------------Play Stompy's Revenge! Now!
Advertisement
It's the pointer that refers to the specific object you are operating on. A class's methods are stored only once in memory, not once per object, so for each object of a particular class, if you want the method to work upon it you need some way of specifying exactly which object. The this pointer is passed around behind the scenes to do exactly that.

Note that static methods of a class are not passed a this pointer, since those methods can be called without having an object to operate on.

Most of the work involving this goes on behind the scenes, and in most routine programming you don't need to fiddle with it or even worry about it. For some things, though, having access to it can be handy. Superpig discusses some such evil, nefarious uses in his latest journal entry...
So I should do this:

 void some_class::doStuff() { this->x += 10; } 


Instead of this:

 void some_class::doStuff() { x += 10; } 


Right?
-----------------------------Play Stompy's Revenge! Now!
Quote:Original post by Stompy9999
So I should do this:

*** Source Snippet Removed ***

Instead of this:

*** Source Snippet Removed ***

Right?

If you exclude the this to access instance members the compiler will automatically include it for you as long as the member name isn't ambiguous (for example, if you had a local variable with the same name as a member variable), so it doesn't really matter. Some people concider it good coding practice to include this, but there are others who think it clutters up code, so go with what makes more sense to you.
I get it now. Thank you both!
-----------------------------Play Stompy's Revenge! Now!
Well, you don't need to. but it can be good to show exactly what you mean, like "x the parameter? x the local variable? no, the this->x of course!" but I rather suggest that you use a better naming convention, like pX for a parameter "x", mX for a class member "x" and just x if you want to use a local variable, that way, the following line can't be confused:

mX = pX;

and it's of course the same as:

this->mX = pX;

but you don't really need this-> now since it's kinda clear what you are doing.

You (at least I) never really need the this-pointer that often, but for instance then you start a thread, you can only give it one parameter (or a struct with more , but to keep this simple), it can be a good idea to throw in the this-pointer of the same class that is creating the thread, hence giving access to all of that classes members within the thread. This is common I think.

Also, the this-pointer can be used for all kinds of things, but I always try to avoid using it as a way of showing which variables are what.

Any other input on this pointers?

Albert
-------------------------------------------http://www.thec.org
I think parameter names are the only time I would use hungarian notation. I agree that having a naming convention is a good idea.
-----------------------------Play Stompy's Revenge! Now!
I've done a good amount of programming, and for 99 percent of the time, You'll likely not need 'this'.

About the only time you use this is when you need to pass a function or a pointer. the place I think of it most is 'return this;'

talking about using it for variables tends to show bad foresight. Never ever use a local variable in a function of a class with the same name as a class variable. In fact I try to keep every variable unique. local variables tend to be "temp____" on my projects. If you get better at naming stuff you'll actually write better code. (I know people who used stupid names like "moo oink and so on" let's just say it's unreadable, and hilarious.)
The most common uses I make of the this pointer is:

SomeFunction( *this ); //such as aVector.push_back( *this )

and, in operators where I need to return a reference to myself:

return *this;
The this pointer is used a lot with operator overloading, especially the assignment operators like = or += and friends:

Foo & Foo::operator = (const Foo &ref) {  // Do stuff.  return *this; }


This is will return a reference to itself, which is useful so you can write statements like a = b = c = d; and have them work as expected.

Another place I would use it is in polymorphic classes. You can't have a virtual copy constructor, so I have a function named clone, which might look like this:

BaseClass * Foo::clone() const {  return new Foo(*this); }


There's a bunch of other times you might need it, but don't worry about them. When the time comes, you'll know it.
Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.

This topic is closed to new replies.

Advertisement