using "this"

Started by
11 comments, last by iMalc 18 years, 8 months ago
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; }
Advertisement
No it does not. The this pointer indirection will happen in either case.
yeah thats what i thought .. just wanted to make sure.. thanks..
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.
"In order to understand recursion, you must first understand recursion."
My website dedicated to sorting algorithms
I just used "this" for the intellisense autocomplete....and occasionally a stray "delete this;"
Adventures of a Pro & Hobby Games Programmer - http://neilo-gd.blogspot.com/Twitter - http://twitter.com/neilogd
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()   {      foo = 42;          // Doesn't work      this->foo = 42;    // Works      Base<T>::foo = 42; // 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
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.
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>

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
Well, you learn something new everyday. I am truly sorry for ever doubting you, oh great Fruny [smile]

This topic is closed to new replies.

Advertisement