Simple Little Question

Started by
5 comments, last by Verg 17 years, 11 months ago
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
}

Advertisement
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.
Dubito, Cogito ergo sum.
Either style will produce the exacty same result, both in code and in speed.
Ok, time for an afternoon of search and replace...

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.
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]

This topic is closed to new replies.

Advertisement