hello,
I have two questions:
1.What is the function of this pointer?
2.Where it is used?Can we avoid to use it?
hello,
I have two questions:
1.What is the function of this pointer?
2.Where it is used?Can we avoid to use it?
What does your C++ book say about it?
when you have a class member function, you can use the 'this' pointer from inside that function to reference the object.
let's say your class has a member function called gets(). there is also a standard C library function called gets() and in order to disambiguate which one you're calling, you could say this->gets(). this may be more for the benefit of the person who must maintain the code, but there are situations where it is necessary to avoid compiler warnings and errors.2.Where it is used?Can we avoid to use it?
I'm a slowpoke but:
the *this-pointer inside a class is good for many things, for example if you got a class-function where you have a parameter with the same name as a class-member, when you want to call a member-function that has the same name as a global one, or when you need to return the class by reference from a function to allow for "chain-function-calls":
dirty example:
("this->member" is also makes it more clear what variable you intend to be changing/using, at least to me)Code:#include <iostream> class MyClass { public: MyClass (int a); MyClass& inc (void); MyClass& print (void); private: int a; }; MyClass::MyClass (int a) { this->a = a; } MyClass & MyClass::inc (void) { (this->a)++; return *this; } MyClass & MyClass::print (void) { std::cout << this->a << std::endl; return *this; } int main (int argc, char *argv[]) { MyClass x (5); x.inc().inc().inc().inc().print(); /* "chain-function-calls" */ return 0; }
Last edited by edoceo; 03-23-2009 at 06:52 AM. Reason: I'm slow.
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
How do you handle this?
The argument cannot have the same name as the member. So you have to either apply a name decoration to the argument, or to the member. You have no choice. Otherwise you end up using "this->" which is what we're trying to avoid.Code:class foo { public: void setValue(int value) { value = value; // ??? } private: int value; };
I don't like Hungarian notation, but applying a special prefix or suffix to member variables seems appropriate.
Code://try //{ if (a) do { f( b); } while(1); else do { f(!b); } while(1); //}
Like this?
However, there is no point going into a flame war over naming conventions.Code:class foo { public: void setValue(int new_value) { value = new_value; // ??? } private: int value; };
The this pointer is simply how you refer to the current instance and it has much more meaningful uses than letting you use the same variable name in the same scope.
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
Originally Posted by anonbrewbuck already covered that by stating "apply a name decoration to the argument".Originally Posted by medievalelks
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Is this a name decoration?
Code:void setValue(int val) { value = val; }
I'm not changing the argument name, I just give it a different name? Would you go out of your way to use the same name for different variables in different scopes? E.g do you feel tempted to use i as the counter for all nested for-loops?
I might be wrong.
Quoted more than 1000 times (I hope).Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.