Jump to content
  • Advertisement
Sign in to follow this  
Infinity95

Classes - Use this-> or access variables directly?

This topic is 2016 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

So im currently doing some self-teaching on c++ and i was wondering if it makes any difference to access variables or other stuff in my classes via the this-> statement or just writing the variable.

Example:

 

class Test {
public:
    int a,b;
    int tester();
private:
    int c,d;
};

int Test::tester() {
    this->a=5;
    this->c=10;
    a = 6;
    return a;
}
 

Here it makes absolutely no difference if i write this->a=somtehing or just simply say a=something

Please tell me what i should use and what difference there is.

 

Thank you in advance!

Edited by Infinity95

Share this post


Link to post
Share on other sites
Advertisement

Thanks for the quick answer! Seems like i'll be using this-> then cause im used to it from php. It's also good that Intellisense kicks in. I like it ;D

Share this post


Link to post
Share on other sites

As he mentioned, it is usually just fine to use this->foo.

 

 

It is not quite universally true, but in the general case the two generate the same code.

 

 

For the first major exception: The rule is not true on objects with an overloaded -> operator.  It is not common to overload the operator, but it occasionally happens.  If the code overrides operator-> then the function will be used rather than the indirection getting optimized away.

 

For the second major exception, it is not true on compilers that don't optimize the indirection away. There are very few of those compilers in use today, but if you happen to be writing code for older devices using older compilers know that it can still travel along the pointer.  You probably won't be coding on the old Nintendo DS, and they probably have fixed their compiler over the years, but know that some rare compilers don't optimize it away.

 

Other than that, use it if you want without feeling guilt.

Share this post


Link to post
Share on other sites

Well i dont think ill run into any problems. Im using VS2012 express for coding and compiling.

Edited by Infinity95

Share this post


Link to post
Share on other sites

Well i dont think ill run into any problems. Im using VS2012 express for coding and compiling.

 

Yes, you will be fine.

 

 

What optimisation is performed to optimise it away? Surely you need to use indirection from the this pointer regardless of whether you use this-> or not?

 

The optimization is that it doesn't need to follow the pointer.

 

It is the difference between

 

mov eax,dword ptr [foo+8]
mov dword ptr [x],eax

 

and this:


mov eax,dword ptr [bar]
mov ecx,dword ptr [eax+8]
mov dword ptr [y],ecx

 

 

Subtle, but important in some cases.  

 

On a modern PC processor, if there is a cache miss you're looking at about 100 nanoseconds to follow the extra indirection.  Most compilers have no problem detecting the situation and avoiding it, and processors with multi-megabyte caches usually have enough room for everything.

 

On an older processor, such as the Nintendo DS (since it was very popular) where you only have about a half million cycles per frame you're wasting cycles on what amounts to a nul-op.  An occasional infraction is okay, but repeat offenses in code add up very quickly.

 

 

Today generally it doesn't matter, unless you are in a situation when you know it matters.

Share this post


Link to post
Share on other sites

Another reason some people also prefer to use 'this->' as a means of stating explicitly that you are accessing the underlying object -- this provides the same function as prepending member variables with '_', 'm', or other markers without having to modify them unnaturally (e.g. mVelocity vs. this->velocity), at the expense of a few more keystrokes.

 

Although it may seem like an important difference, the biggest issue is to just pick one or the other style and be consistent. There are uncommon caveats that frob laid out wonderfully, and you should be aware of them and use them to inform your decision, but the two are always logically equivalent, and on most platforms, most times, the two generate identical code. 

Share this post


Link to post
Share on other sites

For me this-> imposes a problem with readability of the code as most people don't use it when it actually is used it raises a flag in my mind to pay extra attention to the code as something special is usually going on.

 

I am not a fan of prefixing with members with just "_" as it is really easy to start to write code that will resemble STL code and that is just plain simply hard to read because of the manufacturer reserved syntax.

Share this post


Link to post
Share on other sites

I prefer to use `m` before every class member so I can easily recognize my member variables and utilize intellisense. Also using this for every member is making your code a lot bigger and harder to understand.

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!