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

This is a discussion on difference between this->method() and method() within the C++ Programming forums, part of the General Programming Boards category; Hi, I would like to know what is the difference between Code: this->method() and the simple Code: method() I found ...

  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
    21,313
    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
    Version Control System: Bazaar

    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
    21,313
    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
    Version Control System: Bazaar

    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,317
    >> 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,189
    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.
    Until you can build a working general purpose reprogrammable computer out of basic components from radio shack, you are not fit to call yourself a programmer in my presence. This is cwhizard, signing off.

  8. #8
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,288
    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, 06:50 PM
  2. stuck on display method
    By shintaro in forum C++ Programming
    Replies: 2
    Last Post: 02-01-2009, 04:17 PM
  3. Best communication method to thousand childs?
    By Ironic in forum C Programming
    Replies: 8
    Last Post: 11-07-2008, 11:30 PM
  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


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