Advertisement Jump to content
Sign in to follow this  
MTclip

using "this"

This topic is 4915 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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; }

Share this post


Link to post
Share on other sites
Advertisement
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.

Share this post


Link to post
Share on other sites
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();
}

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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).

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Well, you learn something new everyday. I am truly sorry for ever doubting you, oh great Fruny [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!