| Disch (8615) | |
| Why? | |
|
|
|
| Luc Lieber (985) | |||
As opposed to? bool m_boolean?
It works well when auto-generating code. Maybe if the ISO committee would push a certain convention we could avoid things like this all together. | |||
|
Last edited on
|
|||
| Grey Wolf (3232) | |
| Why not? | |
|
|
|
| ResidentBiscuit (2645) | |
Am I wrong, or can you use this.member? And I don't see the issue here. Using this is much cleaner than making up another name for the same data.
| |
|
|
|
| chrisname (6181) | |||
|
@ResidentBiscuit No. Unfortunately someone decided that in C++, this would be a pointer instead of a reference so it has to be this->member or (*this).member. In C# and Java you can use this.member because this is a reference.[edit] I suppose you could do this:
[/edit] @Disch It makes it obvious both to human readers and the compiler that you're referring to an instance member rather than a temporary variable, external function or static member. I don't do it unnecessarily in C++ because I find this-> unattractive, but in C# or Java I always put this. before an instance member.
| |||
|
Last edited on
|
|||
| EssGeEich (1007) | |
In past I used to find this->member nice and attractive. Now I don't anymore, I don't know how it happened.
| |
|
|
|
| NGen (630) | |
| The only thing I can think of is that, if there's no convention for prefixing member variables, it assists the reader in understanding the code. But if you prefix with 'm' or some variant, it's pointless. | |
|
Last edited on
|
|
| Imadatobanisa (647) | |
|
Wow, In past I didn't know (*this) reference... :( Great! Now gererally my code is smarter and looks much better than the old one... Thanks chrisname! :D | |
|
|
|
| TheIdeasMan (1753) | |
| Just so people know - Imadatobanisa is the reincarnated Jackson Marie. | |
|
|
|
| cire (2347) | |||
Fixed! | |||
|
|
|||
| Santosh Reddy (58) | |||
|
Practically adding “this->” prefix will increase the tokenizing and token recognition steps of unit translation during compilation of the code, and will decrease the variable namespace resolution time during code generation phase. So effetely there is no difference. Moreover “this” pointer is not a member of class, for example
Also ever wondered 1. What is access level for “this” (public/private/protected). I know someone will say private, in that case why can’t we access “this” (Private member) from a friend function 2. I think “this” was originally provided to get the address of the class in-side the class methods, to perform some low level byte manipulation possible, later found other uses like name collision avoidance, etc. 3. I think “this” is subjective word. When some says this, it kind of implies that he is not part of “this”. Hmm I guess “me”/“our” is more appropriate. Edit: I ment address of object (isn't it obvious) | |||
|
Last edited on
|
|||
| cire (2347) | |
|
1. You just said it wasn't a member, so how would it have an access level? It is a "hidden" parameter to class methods. 2. this is passed to the function so that the function knows what object it is operating on. Every use of a data member inside a class function uses this implicitly (if it isn't done explicitly.) There isn't any such thing as an address of a class. | |
|
|
|
| coder777 (2549) | |||||
. better than ->? By the way in C#/Java nearly every non trivial data type is a pointer.It's just veiled. This allows to omit the #include because the size of the types are known
this is hiddenly passed as the first parameter to a member function.
| |||||
|
|
|||||
| helios (10258) | ||||||
this is not a member, therefore this question makes as much sense as asking about the circumference of a square.
| ||||||
|
|
||||||
| JLBorges (1754) | |||
this->member is typically used in template code to specify that member is a dependent name.
It is also used in code emitted by many code generators; they don't want to worry about whether the name of a member has been hidden by the surrounding code. And it is used in newbie code which is all written inside an IDE; this-> triggers autocompletion.
| |||
|
Last edited on
|
|||
| EssGeEich (1007) | |
Just pointing out, thiscall implies the this pointer is in the ecx register on 32-bits. So it's not really a common parameter like for stdcall/cdecl/fastcall.
| |
|
|
|
| Imadatobanisa (647) | |
|
@EssGeEich Am I able to call a __thiscall function by assembly? Is ecx a structure parameter which the function will use as a glocal variable? | |
|
Last edited on
|
|
| EssGeEich (1007) | |||||
"Ecx" is a register which is like a very very fast CPU variable. A call to a member function looks like:
| |||||
|
Last edited on
|
|||||
| chrisname (6181) | |||
|
@cire You can use this-> to refer to static members? That seems illogical.
It's not, necessarily; I just think it looks better. I guess you could argue that . doesn't require a pointer dereference which might save you a few CPU cycles. Then again, I'm not sure how references are implemented, so it might not save any time at all, or even take more time. | |||
|
|
|||
| L B (3806) | |||||||
As for prefixing with this-> I don't do it myself because I never have a parameter named impl. Well, I guess impl-> is the same length as this->
| |||||||
|
Last edited on
|
|||||||