Advertisement

this->memberVar VS memberVar

Started by March 14, 2003 06:43 AM
9 comments, last by RizMan 21 years, 8 months ago
I was just wondering: Let''s say I have the following
  
class ClassA{
	int memberVar;
	//member functions etc...

};

//Is it faster to access memberVar using

this->memberVar
//or simply

memberVar
  
A friend of mine told me that using "this" would slow down. However, I thought that the compiler implicitely used the "this" pointer. I am wondering about this because I am systematically using "this" to acces member Variables of a class because I think the code then becomes more readable.
You are correct, the this pointer is used implicitly if you don''t write it. There is no slow down.
Advertisement
However, isntead of writing this->var to show that the var is a member var of the class, set a prefix to the variables like an m_ or just _.


I am a signature virus. Please add me to your signature so that I may multiply.
[s]--------------------------------------------------------[/s]chromecode.com - software with source code
The use of _ as a prefix is reserved (for standard library implementors if I remember correctly). I would not recommend it. I usually preface member variables with m.
Thanks for making this clear. I knew it. I''m gonna show this thread to my friend
Using the this pointer also lets you explicitely refer to members that may have been hidden in some scopes.

class Foo{  int i;public:  void Func( int i )  {     while( i-- > 0 )       this->i += i;  }} 


This example obviously is bad design, but in more complex cases, it may be justified.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Advertisement
Here''s a more common use:


  class A {    int foo;    void SetFoo(int foo)    {        this->foo = foo;    }};  


A lot of people come up with a weird, random alternate spelling of the variable name instead, and it just leads to confusion.

But... but that''s what HITLER would say!!
quote: Original post by Sneftel
Here''s a more common use:

[A simple example follows]



*bangs head on wall*

Why make it simple when you can make it complicated

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
quote: Original post by Fruny
Using the this pointer also lets you explicitely refer to members that may have been hidden in some scopes.


I''m aware of that. Just wanted to know if there was a perfmormance issue using it systematically.
quote: Original post by RizMan
I''m aware of that. Just wanted to know if there was a perfmormance issue using it systematically.


Well, you got your answer

Plus, you can tell your friend that this is not just a pointer. Since it is a keyword, part of the language, compilers can and do treat it specially.

And when in doubt, running performance tests can convince the skeptics

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement