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
|
  |
| 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
|
  |
| 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 |
|
|
|
| 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
|
  |
| 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
|
  |
 |
| 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 |
|
|
|
| 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
|
  |
 |
| 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
|
  |
| 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
|
  |
 |
| 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
|
  |
| Joined: 03 Apr 2011 |
| Total Posts: 5386 |
|
| |
|