Skip to main content
gamedev.net
🔒 Locked

using "this"

Started by MTclip 11 replies 1.1k views
Original Post
MTclip
MTclip
this may seem like a strange question.. but in msvc++ does using "this" inside you class to refer to the class' data have any side effects, like extra lookup / slower performance.. ...just wondering.. example.. CBall::CBall() { m_size = 5; } CBall::CBall() { this->m_size = 5; }
MTclip
MTclip
yeah thats what i thought .. just wanted to make sure.. thanks..
iMalc
iMalc
No, but you only ever need to use "this->" if you have local variables with the same names as members, which may be a bad idea anyway. Using just "this" or "*this" is fine however.
Fruny
Fruny
Quote:
Original post by iMalc
No, but you only ever need to use "this->" if you have local variables with the same names as members, which may be a bad idea anyway.


That's not the only case. If you have a class template and are referring to a base class template member, it is necessary to either specify this-> or properly qualify the name:

template<class T> class Base{public:   int foo;};template<class T> class Derived :   public Base<T>{public:   void do_stuff()   {      <font color=brightred>foo = 42;</font>          // Doesn't work      <font color=lime     >this->foo = 42;</font>    // Works      <font color=lime     >Base<T>::foo = 42;</font> // Works   }};int main(){   Derived<int> bar;   bar.do_stuff();}
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Perost
Perost
Fruny, are you sure that is the case? I tried your code, and it compiled without a warning on g++ 3.3.4 under linux.
Endurion
Endurion
I second Perost, are you sure about that Fruny?

I can do that in my GUI classes (derived from a templated class) and i do not ever need to use this-> to access those members (besides i think it looks fugly).
Fruny: Ftagn! Ia! Ia! std::time_put_byname! Mglui naflftagn std::codecvt eY'ha-nthlei!,char,mbstate_t>
Fruny
Fruny
Quote:
Original post by Perost
Fruny, are you sure that is the case? I tried your code, and it compiled without a warning on g++ 3.3.4 under linux.


Upgrade to 3.4 and see it explode. The free lunch is over. [smile]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Perost
Perost
Well, you learn something new everyday. I am truly sorry for ever doubting you, oh great Fruny [smile]
Fruny
Fruny
Quote:
Original post by Perost
Well, you learn something new everyday. I am truly sorry for ever doubting you, oh great Fruny [smile]


I learned about it when I upgraded and saw the compiler barf on my code. I have no merit - I just found about it, the hard way (much frantic code twiddling and anguished googling), before you did. [wink]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Nemesis2k2
Nemesis2k2
*Adds two-stage name lookup to bag of obscure knowledge*
iMalc
iMalc
Quote:
Original post by Fruny
Quote:
Original post by iMalc
No, but you only ever need to use "this->" if you have local variables with the same names as members, which may be a bad idea anyway.


That's not the only case. If you have a class template and are referring to a base class template member, it is necessary to either specify this-> or properly qualify the name:

      <font color=brightred>foo = 42;</font>          // Doesn't work      <font color=lime     >this->foo = 42;</font>    // Works      <font color=lime     >Base<T>::foo = 42;</font> // Works
Wow, I didn't know about that two-stage name lookup stuff, even though in the past I've had to use the Base<T>:: syntax, (which I prefer actually).
Technically my statement still stands, as it seems that using this-> is merely one of two ways of overcomming the situation. Not meaning to sounds too picky though. Thanks for the info!

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.