Thread: difference between this->method() and method()

  1. #1
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126

    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

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2006
    Location
    japan
    Posts
    126
    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

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2007
    Posts
    416
    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'
    }

  6. #6
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> 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.

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    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.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    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

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

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

Tags for this Thread