Sign in to follow this  
Graham11

Simple Little Question

Recommended Posts

Graham11    122
Hi, In a C++ object member, will my code run slower if I use the 'this' pointer? It seems like a more readable construction, but is there a cost? Cheers, Graham
if (this->Id) {
// Do something
}

//versus

if (Id) {
// Do something
}

Share this post


Link to post
Share on other sites
DadleFish    150
No, using this can clear ambiguities for the compiler but will not make a difference in terms of performance.

Plus another thing - if you want members to be seen clearly, you might want to use the well-accepted "m_" prefix as a convention. In your case you would use m_Id as the name of the member.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
the "this" pointer is implicit for member variables, so no need and no runtime gain by (not) using it.

Share this post


Link to post
Share on other sites
Zahlman    1682
As an aside, I really, really don't understand why anyone finds it "more readable" to type the this->. I personally only use it when needed for disambiguation. As long as you're thinking in terms of writing the member function "from the object's perspective", it really doesn't add any useful information.

Share this post


Link to post
Share on other sites
Verg    450
Quote:
Original post by Zahlman
As an aside, I really, really don't understand why anyone finds it "more readable" to type the this->. I personally only use it when needed for disambiguation. As long as you're thinking in terms of writing the member function "from the object's perspective", it really doesn't add any useful information.


It isn't more readable; but it seems to help MSVC's Intellisense, esp. inside templates.

I prefer not to use "this->", personally.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this