C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 11-21-2009, 03:00 AM   #1
Registered User
 
Join Date: Nov 2006
Location: japan
Posts: 107
Question difference between this->method() and method()

Hi,
I would like to know what is the difference between
Code:
this->method()
and the simple
Code:
method()
I found that both compile and seems to be ok, but I suppose there should be a difference.

Thanks in advance
__________________
Mac OS 10.6 Snow Leopard : Darwin
nacho4d is offline   Reply With Quote
Old 11-21-2009, 03:07 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,923
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?
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 11-21-2009, 04:15 AM   #3
Registered User
 
Join Date: Nov 2006
Location: japan
Posts: 107
Thanks LaserLight,
So a good idea would be to use "this->method()" everytime if possible? (when calling members)
__________________
Mac OS 10.6 Snow Leopard : Darwin
nacho4d is offline   Reply With Quote
Old 11-21-2009, 04:28 AM   #4
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,923
Quote:
Originally Posted by nacho4d
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.
__________________
C + C++ Compiler: MinGW port of GCC
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is online now   Reply With Quote
Old 11-21-2009, 07:35 AM   #5
Registered User
 
Join Date: Mar 2007
Posts: 369
Quote:
Originally Posted by nacho4d View Post
Thanks LaserLight,
So a good idea would be to use "this->method()" everytime if possible? (when calling members)
Quote:
Originally Posted by laserlight View Post
...but the former can be useful if disambiguation of names is needed...
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'
}
__________________
home page (new layout)
scwizzo is offline   Reply With Quote
Old 11-21-2009, 01:55 PM   #6
Registered User
 
Join Date: Jan 2005
Posts: 7,221
>> 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.
Daved is offline   Reply With Quote
Old 11-21-2009, 03:54 PM   #7
Malum in se
 
abachler's Avatar
 
Join Date: Apr 2007
Posts: 3,187
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.
__________________
So let me get this straight. There is a food shortage in Haiti and you want me to donate money instead of food, because its cheaper to ship money to Haiti to buy food locally than to ship the food there. So where the ........ is the shortage if they have the food there already? As usual - "If money can fix the problem, then people are intentionally causing the problem".
abachler is offline   Reply With Quote
Old 11-21-2009, 04:11 PM   #8
Algorithm Dissector
 
iMalc's Avatar
 
Join Date: Dec 2005
Location: New Zealand
Posts: 2,654
Quote:
Originally Posted by Daved View Post
>> 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.
E.g.
Code:
foo(a, b, c);
vs
Code:
foo(this->a, this->b, this->c);
etc...

You wont catch me putting tons of redundant clutter in there!
__________________
My homepage
Advice: Take only as directed - If symptoms persist, please see your debugger
iMalc is offline   Reply With Quote
Reply

Tags
this usage?

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
on method pointers and inheritance BrownB C++ Programming 2 03-02-2009 07:50 PM
stuck on display method shintaro C++ Programming 2 02-01-2009 05:17 PM
Best communication method to thousand childs? Ironic C Programming 8 11-08-2008 12:30 AM
C# method siten0308 C# Programming 6 07-15-2008 07:01 AM
Overriding a method in C DavidDobson C Programming 1 07-05-2008 07:51 AM


All times are GMT -6. The time now is 09:04 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22