🔒 Locked
using "this"
Advertisement
Original Post
No it does not. The this pointer indirection will happen in either case.
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();}
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]
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]
*Adds two-stage name lookup to bag of obscure knowledge*
Quote: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).
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
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.
Advertisement