MudBytes
Pages: << prev 1 next >>
this
JohnnyStarr
Wizard






Group: Members
Posts: 953
Joined: Feb 14, 2009

Go to the bottom of the page Go to the top of the page
#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);
}
 

kiasyn
Wizard






Group: Administrators
Posts: 1,109
Joined: May 15, 2006

Go to the bottom of the page Go to the top of the page
#2 id:48257 Posted Jul 13, 2010, 4:13 pm

if you make a local variable called carry_number or carry_weight then you use that it will assign the value to the local variable rather than the member of the object, unless you explicitly declare this->
.........................
http://www.mudbytes.net/kiasyn-sig.png

http://portal.hypernia.com/banners/hypernia_button.jpg http://www.shastaherps.org/badges/linode88.png

Kline
Wizard






Group: Members
Posts: 724
Joined: Dec 14, 2007

Go to the bottom of the page Go to the top of the page
#3 id:48258 Posted Jul 13, 2010, 4:16 pm

Would enabling shadow variable in warnings prevent that? I've used / omitted this-> for my own readability's sake in various places but never gave consideration about a local variable named something in the member class (and haven't had that problem; yet).
.........................
AckFUSS -- Check it out.

kiasyn
Wizard






Group: Administrators
Posts: 1,109
Joined: May 15, 2006

Go to the bottom of the page Go to the top of the page
#4 id:48259 Posted Jul 13, 2010, 7:44 pm

I think it does... not sure.
.........................
http://www.mudbytes.net/kiasyn-sig.png

http://portal.hypernia.com/banners/hypernia_button.jpg http://www.shastaherps.org/badges/linode88.png

Kaz
Conjurer






Group: Members
Posts: 125
Joined: Oct 31, 2009

Go to the bottom of the page Go to the top of the page
#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

Go to the bottom of the page Go to the top of the page
#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:
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Kaz
Conjurer






Group: Members
Posts: 125
Joined: Oct 31, 2009

Go to the bottom of the page Go to the top of the page
#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

Go to the bottom of the page Go to the top of the page
#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.
.........................
-- d.c.h --
BabbleMUD Project (custom codebase)
Legends of the Darkstone (head coder)
http://david.the-haleys.org
.........................

Last edited Jul 14, 2010, 9:06 am by David Haley
Pages:<< prev 1 next >>

Valid XHTML 1.1! Valid CSS!