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?
🔒 Locked
[C++] Using this pointer?
Advertisement
Original Post
Is there any gotcha or associated overhead with using the "this" pointer too frequent? For example, the code below:
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
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:
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 XIf you write code like that in C++, you should probably be flogged in public.
{
int value;
void setValue( int value )
{
this->value = value;
}
}
quote:
Original post by Oluseyiclass XIf you write code like that in C++, you should probably be flogged in public.
{
int value;
void setValue( int value )
{
this->value = value;
}
}
I wouldn''t write code like that but some coding standards use that style.
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)
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
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.
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.
quote:
Original post by davepermenquote:
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
if you know python I needn''t draw the comparison
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
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
Maybe you should try decaf.
--
Dave Mikesell Software
quote: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.
Original post by Oluseyiclass XIf you write code like that in C++, you should probably be flogged in public.
{
int value;
void setValue( int value )
{
this->value = value;
}
}
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
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.
Advertisement