Hi,
I would like to know what is the difference between
and the simpleCode:this->method()
I found that both compile and seems to be ok, but I suppose there should be a difference.Code:method()
Thanks in advance ;)
Printable View
Hi,
I would like to know what is the difference between
and the simpleCode:this->method()
I found that both compile and seems to be ok, but I suppose there should be a difference.Code:method()
Thanks in advance ;)
Assuming that member is a member function and the call is from within a member function of the class, they are usually equivalent, but the former can be useful if disambiguation of names is needed, or in another related case: Why am I getting errors when my template-derived-class uses a member it inherits from its template-base-class?
Thanks LaserLight,
So a good idea would be to use "this->method()" everytime if possible? (when calling members)
In my opinion, no, since it is usually just clutter.Quote:
Originally Posted by nacho4d
I believe what laser light means by this is something similar to the following.
Code:class foo
{
private:
int milliseconds;
public:
void time(int);
};
void foo::time(int milliseconds)
{
this->milliseconds = milliseconds; // versus saying 'milliseconds = milliseconds'
}
>> In my opinion, no, since it is usually just clutter.
I agree. In fact, if you don't use this-> unless it is necessary, it helps make it clearer that it is necessary if you ever do use it.
I use it every time, but hen I prefer my code to be clear and concise and easily maintainable. If you leave this-> out, you may regret it some day when you make changes that make it necessary and you have to track through 1000 lines of code to find the problem.