Skip to main content
gamedev.net
🔒 Locked

[C++] Using this pointer?

Started by LonelyTower Mar 3, 2004 at 8:55 AM 13 replies 7k views
Original Post
LonelyTower
LonelyTower
Is there any gotcha or associated overhead with using the "this" pointer too frequent? For example, the code below:
class A
{
public:
A();
~A();

int variable;

int print() { return this->variable }

}
 
The only reason to do this so that I can easily differnate member variables/functions. Is this advisable?
"Magic makes the world go round." - Erasmus, Quest For Glory I
dmikesell
dmikesell
quote:
Original post by LonelyTower
Is there any gotcha or associated overhead with using the "this" pointer too frequent? For example, the code below:


class A
{
public:
A();
~A();

int variable;

int print() { return this->variable }

}


The only reason to do this so that I can easily differnate member variables/functions. Is this advisable?


It''s a matter of preference. I prefer not to, but others I''m sure prefer to fully qualify members with this->.



--
Dave Mikesell Software
Oluseyi
Oluseyi
There''s no performance penalty, since you''re merely making the implicit explicit.

It can be used to distinguish between parameters that share the names of member variables, though many would recommend changing one of the identifiers:
class X
{
int value;
void setValue( int value )
{
this->value = value;
}
}
If you write code like that in C++, you should probably be flogged in public.
Qw3r7yU10p!
Qw3r7yU10p!
quote:
Original post by Oluseyi
class X
{
int value;
void setValue( int value )
{
this->value = value;
}
}
If you write code like that in C++, you should probably be flogged in public.


I wouldn''t write code like that but some coding standards use that style.
simba
simba
Why not use m_ for member variables? This seems to be the most common in the industry.
ironfroggy
ironfroggy
the this pointer is being dereferenced to find member variables no matter what, or, rather, the offsetted pointer of this + the member offset. basically, you could just assume that this->foo is computationally identical to foo, assuming foo is a member and there are no other foo symbols above the scope of the class members (such as function arguments)
(http://www.ironfroggy.com/)(http://www.ironfroggy.com/pinch)
davepermen
davepermen
quote:
Original post by simba
Why not use m_ for member variables? This seems to be the most common in the industry.


and why not use this-> instead of m_ ? its the same. just one is a language feature, one is just a bad habbit.




If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
Allmight
Allmight
You can safely use the this pointer without any penalty hits.
In fact, if you dont the compiler automatically assumes that
the this pointer is there, and adds it at compile time.

So what it all comes down to in the end is the matter of taste.
I usually dont use the this pointer unless i want to pass a
pointer to the class/object to some outside function or similar.
-------------------------------------------------Founder and DirectorAllSoft Studios
dmikesell
dmikesell
quote:
Original post by davepermen
quote:
Original post by simba
Why not use m_ for member variables? This seems to be the most common in the industry.


and why not use this-> instead of m_ ? its the same. just one is a language feature, one is just a bad habbit.



Sorry, but I prefer something like this:

volume_ = x_ * y_ * z_;

to

this->volume = this->x * this->y * this->z;

Creating code that''s hard to read is a bad habit.



--
Dave Mikesell Software
JOL
JOL
using the this pointer is NOT bad, it is sometimes a very good way of indicating exactly what you mean... you dont mean value=10, you mean this->value=10 ... there is a ''slight'' difference and sometimes that makes your code clearer...
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~I'm looking for work
Qw3r7yU10p!
Qw3r7yU10p!
if you know python I needn''t draw the comparison
davepermen
davepermen
quote:
Original post by dmikesell
Sorry, but I prefer something like this:

volume_ = x_ * y_ * z_;

to

this->volume = this->x * this->y * this->z;

Creating code that''s hard to read is a bad habit.



--
Dave Mikesell Software


and i prefer

volume = x*y*z;

over your garbage.

you know, you don''t need the this pointer most the time. only when interfering with parameters you gave in with the same names, a.k.a. in setter functions (constructors, and such).

and if you have good coding habbits, than that isn''t much code normally, and split from such volume calc code.

the this-> pointer is the best solution, if you learn how to code in c++.

most other languages use this. very often (c#, java i guess as well, D for sure, etc..) to access the members. why do you feel bether in not doing it, but instead invent some proprietary convention to accomplish the same, and loosing readability.

a lot of m_x or x_ cluttered all over the place are not really THAT readable. x is.




If that''s not the help you''re after then you''re going to have to explain the problem better than what you have. - joanusdmentia

davepermen.net
If that's not the help you're after then you're going to have to explain the problem better than what you have. - joanusdmentia
My Page davepermen.net | My Music on Bandcamp and on Soundcloud
dmikesell
dmikesell
Whatever. It''s a matter of opinion. I''ll bow out of this as you seem to want to make it personal, and I''d rather not go there.

Maybe you should try decaf.

--
Dave Mikesell Software
Cedric
Cedric
quote:
Original post by Oluseyi
class X
{
int value;
void setValue( int value )
{
this->value = value;
}
}
If you write code like that in C++, you should probably be flogged in public.
Eh, I don''t usually use it, but for some reasons, I did in my current project (for the constructors), and I got my virtual flogging yesterday.

I made a typo in the parameter definition:

void setValue(int vaalue)
{
this->value = value;
}

It was C#, actually, and it didn''t complain about the unused parameter, unfortunately.

Cédric
TangentZ
TangentZ
quote:
Original post by Cedric

void setValue(int vaalue)
{
this->value = value;
}


It was C#, actually, and it didn't complain about the unused parameter, unfortunately.



Heh, very nice example of why it is a bad idea to
use the same name for a function parameter and a member
variable.

Hmmm... But shouldn't the compiler complain that the variable
is used before being initialized? (I guess this only applies
to constructors.)

*shrug*



Kami no Itte ga ore ni zettai naru!

[edited by - tangentz on March 3, 2004 12:22:44 PM]
神はサイコロを振らない!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.