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. what does "this" mean?

what does "this" mean?

Scheduled Pinned Locked Moved C / C++ / MFC
question
9 Posts 7 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.
  • D Offline
    D Offline
    Deepak Samuel
    wrote on last edited by
    #1

    I just encountered this "this" in a sample code. Can anyone explain what it means?. _param->_this = this; Is the "this" an operator? Thanks. Deepak Samuel

    M D V T 5 Replies Last reply
    0
    • D Deepak Samuel

      I just encountered this "this" in a sample code. Can anyone explain what it means?. _param->_this = this; Is the "this" an operator? Thanks. Deepak Samuel

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      The this pointer refers to the object in which it is invoked.


      "When I was born I was so surprised that I didn't talk for a year and a half." - Gracie Allen

      1 Reply Last reply
      0
      • D Deepak Samuel

        I just encountered this "this" in a sample code. Can anyone explain what it means?. _param->_this = this; Is the "this" an operator? Thanks. Deepak Samuel

        M Offline
        M Offline
        Maximilien
        wrote on last edited by
        #3

        this is the current object instance. you can do stuff like that...

        class CClass
        {
        int i;
        void Method();
        }

        void CClass::Method()
        {
        // equivalent.
        this->i = 1;
        i = 1;
        }

        or if you you want to pass yourself to another class or method :

        ...
        CPaintDC dc(this);
        ...


        Maximilien Lincourt Your Head A Splode - Strong Bad

        1 Reply Last reply
        0
        • D Deepak Samuel

          I just encountered this "this" in a sample code. Can anyone explain what it means?. _param->_this = this; Is the "this" an operator? Thanks. Deepak Samuel

          V Offline
          V Offline
          V 0
          wrote on last edited by
          #4

          "this" is introduced so the compiler could make the difference between members of the class and members of another object. eg. if you would do this: public void SetSomeInt(int input){ input = input; } then the compiler probably would complain. which one is the parameter and which one is the variable of the class? if you do this: public void SetSomeInt(int input){ this->input = input; } it will probably work. It's just a pointer to the Object itself. "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix

          1 Reply Last reply
          0
          • D Deepak Samuel

            I just encountered this "this" in a sample code. Can anyone explain what it means?. _param->_this = this; Is the "this" an operator? Thanks. Deepak Samuel

            V Offline
            V Offline
            V 0
            wrote on last edited by
            #5

            "this" is introduced so the compiler could make the difference between members of the class and members of another object. eg. if you would do this: public void SetSomeInt(int input){ input = input; } then the compiler probably would complain. which one is the parameter and which one is the variable of the class? if you do this: public void SetSomeInt(int input){ this->input = input; } it will probably work. It's just a pointer to the Object itself. (and that's the reason why you can't use it in static functions) "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix

            A 1 Reply Last reply
            0
            • V V 0

              "this" is introduced so the compiler could make the difference between members of the class and members of another object. eg. if you would do this: public void SetSomeInt(int input){ input = input; } then the compiler probably would complain. which one is the parameter and which one is the variable of the class? if you do this: public void SetSomeInt(int input){ this->input = input; } it will probably work. It's just a pointer to the Object itself. (and that's the reason why you can't use it in static functions) "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix

              A Offline
              A Offline
              Antony M Kancidrowski
              wrote on last edited by
              #6

              I would advise that you *DO NOT* use the same naming convention for local function variables and member variables. It can, and will, always lead to confusion and mistakes. Ant. I'm hard, yet soft.
              I'm coloured, yet clear.
              I'm fruity and sweet.
              I'm jelly, what am I? Muse on it further, I shall return!
              - David Williams (Little Britain)

              V M 2 Replies Last reply
              0
              • D Deepak Samuel

                I just encountered this "this" in a sample code. Can anyone explain what it means?. _param->_this = this; Is the "this" an operator? Thanks. Deepak Samuel

                T Offline
                T Offline
                toxcct
                wrote on last edited by
                #7

                you've been having many good answers, so i won't add more about the this pointer you're asking for. i'd just like to tell you that this is an important part of the C++ language, and you'd so better get a reference of the language to learn about it... there are many books on the subject, and also the MSDN treats about it. cheers,:rose:


                TOXCCT >>> GEII power

                1 Reply Last reply
                0
                • A Antony M Kancidrowski

                  I would advise that you *DO NOT* use the same naming convention for local function variables and member variables. It can, and will, always lead to confusion and mistakes. Ant. I'm hard, yet soft.
                  I'm coloured, yet clear.
                  I'm fruity and sweet.
                  I'm jelly, what am I? Muse on it further, I shall return!
                  - David Williams (Little Britain)

                  V Offline
                  V Offline
                  V 0
                  wrote on last edited by
                  #8

                  Quite true. Just to have no misunderstandings : "I do not do that" :), but the example just explains nicely what 'this' means. "If I don't see you in this world, I'll see you in the next one... and don't be late." ~ Jimi Hendrix

                  1 Reply Last reply
                  0
                  • A Antony M Kancidrowski

                    I would advise that you *DO NOT* use the same naming convention for local function variables and member variables. It can, and will, always lead to confusion and mistakes. Ant. I'm hard, yet soft.
                    I'm coloured, yet clear.
                    I'm fruity and sweet.
                    I'm jelly, what am I? Muse on it further, I shall return!
                    - David Williams (Little Britain)

                    M Offline
                    M Offline
                    Michael P Butler
                    wrote on last edited by
                    #9

                    Antony M Kancidrowski wrote: I would advise that you *DO NOT* use the same naming convention for local function variables and member variables. It can, and will, always lead to confusion and mistakes. That's why they invented m_ :-D Michael CP Blog [^]

                    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