if (this->Id) {
// Do something
}
//versus
if (Id) {
// Do something
}
Simple Little Question
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
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.
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.
Ok, time for an afternoon of search and replace...
Btw I do use m_, just not in that example...
Thanks guys,
Graham
Btw I do use m_, just not in that example...
Thanks guys,
Graham
the "this" pointer is implicit for member variables, so no need and no runtime gain by (not) using it.
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.
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.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement