JohnnyStarr
Wizard


Group: Members
Posts: 953
Joined: Feb 14, 2009
|
#1 id:48256 Posted Jul 13, 2010, 3:47 pm
|
It makes sense to me that if an object needed to pass itself to an outside function, it would send "this".
I've seen some C++ that uses "this" anytime it accesses a property. Does that protect the author from some
sort of folly?
An example might be:
Code (C++): 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27 |
void Character::getObject(Object * obj)
{
obj->next_content = this->carrying;
this->carrying = obj;
obj->carried_by = this;
obj->in_room = NULL;
obj->in_obj = NULL;
this->carry_number += get_obj_number(obj);
this->carry_weight += get_obj_weight(obj);
}
// VS.
void Character::getObject(Object * obj)
{
obj->next_content = carrying;
carrying = obj;
obj->carried_by = this;
obj->in_room = NULL;
obj->in_obj = NULL;
carry_number += get_obj_number(obj);
carry_weight += get_obj_weight(obj);
}
|
|
|
|
|
|
|
|
|
|
Kaz
Conjurer


Group: Members
Posts: 125
Joined: Oct 31, 2009
|
#5 id:48265 Posted Jul 14, 2010, 1:36 am
|
JohnnyStarr said:It makes sense to me that if an object needed to pass itself to an outside function, it would send "this".
I've seen some C++ that uses "this" anytime it accesses a property. Does that protect the author from some
sort of folly?
For the most part, it's a stylistic thing. I find it "noisy", personally. The only time it's actually necessary is when your code is looking at a member variable for a class template who inherited from one of its template parameters. For example:
Code (C++): 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 |
template <class Base>
struct Derived : Base
{
Derived()
{
// Without "this->", the compiler must assume that foo_ is
// a non-member variable, because it can't see it in the current
// scope during that compilation stage. With this->, it now knows
// that it is a member variable from the inherited base class.
this->foo_ = true;
}
};
|
|
Last edited Jul 14, 2010, 1:37 am by Kaz
|
|
David Haley
Wizard


Group: Members
Posts: 7,783
Joined: Jun 30, 2007
|
#6 id:48267 Posted Jul 14, 2010, 6:58 am
|
Kaz said:The only time it's actually necessary is when your code is looking at a member variable for a class template who inherited from one of its template parameters.
Or when there is shadowing, as was mentioned.
Code (text): 1
2
3
4
5 | void Foo::bar(int x) {
this->x = x;
} |
I usually just use a naming convention that avoids the problem, by adding a _ suffix to member variables. Some people hate that, though, and some people hate using 'this'. :shrug:
|
|
|
Kaz
Conjurer


Group: Members
Posts: 125
Joined: Oct 31, 2009
|
#7 id:48270 Posted Jul 14, 2010, 8:26 am
|
David Haley said:Kaz said:The only time it's actually necessary is when your code is looking at a member variable for a class template who inherited from one of its template parameters.
Or when there is shadowing, as was mentioned.
At which time you can also change the names of the variables involved in order to avoid the this->. You can't do that with the case I showed.
Incidentally, I also add an underscore suffix. However, if I were a regular Visual Studio person, however, I'd probably use the time-honoured m_ as it plays more nicely with Intellisense.
|
|
|
David Haley
Wizard


Group: Members
Posts: 7,783
Joined: Jun 30, 2007
|
#8 id:48271 Posted Jul 14, 2010, 9:06 am
|
Quote:At which time you can also change the names of the variables involved in order to avoid the this->. You can't do that with the case I showed.
Sure, that's true. But if you change names, you start running into somewhat silly cases like:
Code (text): 1
2
3
4
5 | void Foo::bar(int otherX) {
x = otherX;
} |
or 'aX', 'theX', 'newX', etc. My preference is to leave variable names as clear as possible, and use 'this' to disambiguate.
|
Last edited Jul 14, 2010, 9:06 am by David Haley
|
|