Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. this-> question

this-> question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++javaperformance
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Frank Liao
    wrote on last edited by
    #1

    Hi All, I was just wondering about something that has been bugging me ever since I saw something like this in Java. In practice, which type of statement would be more preferable if you were to call a class function inside the same class in C++?

    class Object {
    void func1() { this->func2() }
    void func2() {}
    };

    or

    class Object {
    void func1() { func2() }
    void func2() {}
    };

    Is there any performance differences between the two? :confused: Also, I apologize if this code might look wrong. I just wanted a conceptual opinion.:) Frank

    C T 2 Replies Last reply
    0
    • F Frank Liao

      Hi All, I was just wondering about something that has been bugging me ever since I saw something like this in Java. In practice, which type of statement would be more preferable if you were to call a class function inside the same class in C++?

      class Object {
      void func1() { this->func2() }
      void func2() {}
      };

      or

      class Object {
      void func1() { func2() }
      void func2() {}
      };

      Is there any performance differences between the two? :confused: Also, I apologize if this code might look wrong. I just wanted a conceptual opinion.:) Frank

      C Offline
      C Offline
      Carlos Antollini
      wrote on last edited by
      #2

      No direrences.... Best Regards... Carlos Antollini.

      1 Reply Last reply
      0
      • F Frank Liao

        Hi All, I was just wondering about something that has been bugging me ever since I saw something like this in Java. In practice, which type of statement would be more preferable if you were to call a class function inside the same class in C++?

        class Object {
        void func1() { this->func2() }
        void func2() {}
        };

        or

        class Object {
        void func1() { func2() }
        void func2() {}
        };

        Is there any performance differences between the two? :confused: Also, I apologize if this code might look wrong. I just wanted a conceptual opinion.:) Frank

        T Offline
        T Offline
        Tomasz Sowinski
        wrote on last edited by
        #3

        There's no performance difference. IMHO, you only need 'this->' when one of the method parameters has name colliding with name of another data member. In the example below, the parameter 'foo' hides the member 'foo', so you need to explicitly qualify it with 'this'. Of course, you can rename the SetFoo parameter 'foo' to 'theFoo', in such case there's no need for explicit 'this'.

        class obj
        {
        public:
        void SetFoo(int foo) { this->foo = foo; }
        int GetFoo() const { return foo; }
        private:
        int foo;
        };

        Tomasz Sowinski -- http://www.shooltz.com

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups