generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Re: Replicating the `this` pointer

Previous Thread :: Next Thread 
Flurite is not online. Flurite
Joined: 03 Apr 2011
Total Posts: 5386
27 Apr 2012 03:19 PM
Hi,

The `this` pointer in C++ is a pointer that points to a _hypothetical_ object. If I am correct, if I have a definition of class X, the `this` pointer does not point to that class X (anywhere where you would actually want to?), but points to an object of the class.

I was just wondering how C++ uses the `this` timer. Does it create a anonymous class of the class it's pointing to? Wut? Also, why would you ever want to point to a class itself, i.e:

class X
{
/* code */
};

X* asdf = &X;
Report Abuse
Varp is not online. Varp
Joined: 18 Nov 2009
Total Posts: 5333
27 Apr 2012 03:32 PM
Classes are not objects. They do not exist at runtime (okay, sort of in RTTI, but that's pretty different).

I don't think you understand what the this pointer is. It's basically an argument passed to all member functions. In practice I haven't seen it used too much So, you have:

class object;
int collide(object*,object*);
...
class object{
public:
...
int doStuff(){
return collide(this,this);
}
...
};
...
int doStuff(object* x){
return collide(x,x);
}

Then the calls

x.doStuff();
and
doStuff(&x);

are the same. It's basically an argument that exists in all member functions, but is hidden. All places where the this pointer exists, it points to a real instance of the class that you, at some point created.
Report Abuse
Flurite is not online. Flurite
Joined: 03 Apr 2011
Total Posts: 5386
27 Apr 2012 03:42 PM
@Varp,

Hmm.. well in my member function definitions, I use the `this` pointer all the time. I understand its hidden pointer usage, but for example, when I try and differentiate between two variables, like this:

int value = 0;

class X
{
public:
int value;
void SetValue(int value) { this->value = value; }
};

Otherwise, I may get an error, or something unexpected will happen, if I do not use the this pointer. But as you see, there is no object (or is there?).
Report Abuse
trappingnoobs is not online. trappingnoobs
Joined: 05 Oct 2008
Total Posts: 19100
27 Apr 2012 04:05 PM
"void SetValue(int value) { this->value = value; }"

You should neeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeever do that

void SetValue(int Value) { value = Value; }

is muchhhhhhhhhhhhhhhhh better
Report Abuse
Flurite is not online. Flurite
Joined: 03 Apr 2011
Total Posts: 5386
27 Apr 2012 04:27 PM
but i have a glitch in my system that causes everything for me to be consistent.

umm..
Report Abuse
stravant is not online. stravant
Forum Moderator
Joined: 22 Oct 2007
Total Posts: 2893
27 Apr 2012 04:29 PM
Also, if you're wondering how it works at the actual hardware level, typically C++ compilers will always store the "this" pointer in the "ecx" register of the processor when they do the call to the method, so you can really think of the function:

int Adder::add(...)
as:
int Adder::add(Adder *const this, ...)

That is, the "this" is an extra argument of sorts that's automatically added to any methods. Things get complicated when you have virtual functions, templates and virtual function pointers thrown in there, but that's the general idea.
Report Abuse
trappingnoobs is not online. trappingnoobs
Joined: 05 Oct 2008
Total Posts: 19100
27 Apr 2012 04:30 PM
you could also do _value as the argument (Or as the actual member and regular value as the arg) it's totally up to you but it's best to keep naming conventions the same cross-app (Obvious reasons) and also not use the same for a member and argument.
Report Abuse
stravant is not online. stravant
Forum Moderator
Joined: 22 Oct 2007
Total Posts: 2893
27 Apr 2012 04:35 PM
Or better yet use "value_" as the member. It's often nice to use coding conventions so that the naming of locals and members never collides. The two that I've seen used most commonly are:
memberName_
and:
myMemberName / m_MemberName
Report Abuse
Flurite is not online. Flurite
Joined: 03 Apr 2011
Total Posts: 5386
27 Apr 2012 04:40 PM
Oh, so `this` does not mean much until you actually run the function (i think this is different then building/compiling).
Report Abuse
stravant is not online. stravant
Forum Moderator
Joined: 22 Oct 2007
Total Posts: 2893
27 Apr 2012 05:25 PM
Yeah, `this` is an entirely run-time value. It is different for every single instance that you might call the method on.

That's also why static methods have no 'this', since they are not called on an instance of the class.
Report Abuse
Flurite is not online. Flurite
Joined: 03 Apr 2011
Total Posts: 5386
27 Apr 2012 06:24 PM
yey, thanks for info
Report Abuse
Previous Thread :: Next Thread 
Page 1 of 1
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image