Sign in to follow this  
Stompy9999

"this" pointer

Recommended Posts

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!

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.)

Share this post


Link to post
Share on other sites
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;

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this