hello,
I have two questions:
1.What is the function of this pointer?
2.Where it is used?Can we avoid to use it?
Printable View
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.Quote:
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;
}
This is why it's a good reason to name the member variables with a prefix, such as m_.
And empty functions (ie functions taking no arguments) don't need void in the declaration.
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.
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.
Quote:
Originally Posted by anon
brewbuck already covered that by stating "apply a name decoration to the argument".Quote:
Originally Posted by medievalelks
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?