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. Really Dumb (easy) Question

Really Dumb (easy) Question

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++
13 Posts 9 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.
  • R Roger Wright

    I'm not generally known as a stupid guy, but I'm really confused by this. I've been programming for 25+ years, but I've only recently tried to add C++ to my list of proficiencies, and I'm embarrassed to ask, but I'm going to anyway. I see code that uses the . operator to reference member functions as often as I see the -> operator used for what appears to me to be the same purpose. What the heck is the difference? Can someone explain to me, without the utterly useless Microsoft technical babble, what the difference in usage is? I know it's trivial to you experts, but I have a (hopefully) temporary blind spot that is blocking me from progressing on my journey of discovery on the path of C++ enlightenment. Please use small words appropriate to my small understanding for your reply... And, Thank you, for helping me to overcome this silly mental block. I'm usually good at spotting patterns, but this one has me stumped.:-O "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.

    L Offline
    L Offline
    lobanovski
    wrote on last edited by
    #4

    ok :) small words? you got em :) // a header file class A { public: A(); ~A(); void foo(); }; // a source file int main(int, char**) { A a_object; A* a_[ointer_to_the_object = new A(); a_object.foo(); a_pointer_to_the_object->foo(); return 0; } you see? when you deal with a pointer - you use -> operator...... debug - is my life style

    1 Reply Last reply
    0
    • R Roger Wright

      I'm not generally known as a stupid guy, but I'm really confused by this. I've been programming for 25+ years, but I've only recently tried to add C++ to my list of proficiencies, and I'm embarrassed to ask, but I'm going to anyway. I see code that uses the . operator to reference member functions as often as I see the -> operator used for what appears to me to be the same purpose. What the heck is the difference? Can someone explain to me, without the utterly useless Microsoft technical babble, what the difference in usage is? I know it's trivial to you experts, but I have a (hopefully) temporary blind spot that is blocking me from progressing on my journey of discovery on the path of C++ enlightenment. Please use small words appropriate to my small understanding for your reply... And, Thank you, for helping me to overcome this silly mental block. I'm usually good at spotting patterns, but this one has me stumped.:-O "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.

      A Offline
      A Offline
      alex barylski
      wrote on last edited by
      #5

      Roger Wright wrote: see code that uses the . operator to reference member functions as often as I see the -> operator used for what appears to me to be the same purpose. What the heck is the difference? To the best of MY understanding :) The member selection operator -> is used whenever the expression before is coupled with an indirection operator *

      // No indirection
      CMyObject myObj;
      myObj.Test();

      // Using indirection
      CMyObject* myObj;
      myObj->Test();

      You can acheive the same thing using

      CMyObject myObj;

      (*myObj).Test();

      Basically when you use -> or *. are accessing members indirectly with a pointer. The word of the day is legs, let's go back to my house and spread the word ;P

      R 1 Reply Last reply
      0
      • A alex barylski

        Roger Wright wrote: see code that uses the . operator to reference member functions as often as I see the -> operator used for what appears to me to be the same purpose. What the heck is the difference? To the best of MY understanding :) The member selection operator -> is used whenever the expression before is coupled with an indirection operator *

        // No indirection
        CMyObject myObj;
        myObj.Test();

        // Using indirection
        CMyObject* myObj;
        myObj->Test();

        You can acheive the same thing using

        CMyObject myObj;

        (*myObj).Test();

        Basically when you use -> or *. are accessing members indirectly with a pointer. The word of the day is legs, let's go back to my house and spread the word ;P

        R Offline
        R Offline
        Roger Wright
        wrote on last edited by
        #6

        Thank you for the only marginally comprehensible answer of the lot. Frankly, pointers drive me over the edge, and make me long for the resurgence of Pascal, the only language that's made any sense in the past 20 years or so. Indirection was handy in the ASM days, and I loved using it. But it seems an absurd layer of obscurity when applied to modern languages. I really hate that part of C++. "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.

        A A J D 4 Replies Last reply
        0
        • R Roger Wright

          Thank you for the only marginally comprehensible answer of the lot. Frankly, pointers drive me over the edge, and make me long for the resurgence of Pascal, the only language that's made any sense in the past 20 years or so. Indirection was handy in the ASM days, and I loved using it. But it seems an absurd layer of obscurity when applied to modern languages. I really hate that part of C++. "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.

          A Offline
          A Offline
          Antti Keskinen
          wrote on last edited by
          #7

          Well, perhaps I'll add my knowledge to the heap. Consider the following: you have an object of some type. Let's name this type 'Roger', and let it have an attribute called 'Age' (No sarcasm intended !) Now, if in your code, you created a static object, you would use Roger MyRoger declaration. This would create a static object for you. You would then access the member variable by using MyRoger.Age = 30, for example. Let's take a step beyond and consider a pointer. See in front of yourself a 3D-space. In this space, you see the MyRoger-object standing at some point. Then, you create a pointer of type Roger using a declaration Roger* pToMyRoger = &MyRoger. Now, the pToMyRoger pointer points towards the MyRoger object, just like an extended finger. A static object is aware of two things: what it is, and where it resides. A pointer to any object is aware of two things as well: where it resides, and what is to be found in that location. For a more in-depth scenario, consider that your Roger-object (MyRoger) reserves a place for itself in memory (&MyRoger). If you issued MyRoger.Age, the compiler would get the address of MyRoger, move forwards from there to the Age variable, and retrieve it's contents. If you used a pointer, then the compiler would take the address you give to that pointer, and ASSUME that the object found from that address is the one specified by the type of the pointer (Roger*). It would then search for the Age variable again, and retrieve it. Then to the term 'dereferencing'. When you have pointer to the object Roger (pToMyRoger), you can dereference it to an OBJECT of type Roger by using the asterisk (*) operator. For example, this declaration (*pToMyRoger).Age is the same thing as MyRoger.Age. As you might guess, *pToMyRoger equals to MyRoger hence leading us to the following conclusion: the dereferencing operator (*) can sometimes be named as the 'contents-of' operator. Thus, the contents of pToMyRoger (*pToMyRoger) equals to the object MyRoger (MyRoger). Does this make sense ? In the conclusion, the '.' and the '->' operators behave similarly, but they are used in different contexts. For example, the following declarations do all the same things MyRoger.Age = (*pToMyRoger).Age = pToMyRog

          R 1 Reply Last reply
          0
          • R Roger Wright

            Thank you for the only marginally comprehensible answer of the lot. Frankly, pointers drive me over the edge, and make me long for the resurgence of Pascal, the only language that's made any sense in the past 20 years or so. Indirection was handy in the ASM days, and I loved using it. But it seems an absurd layer of obscurity when applied to modern languages. I really hate that part of C++. "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.

            A Offline
            A Offline
            alex barylski
            wrote on last edited by
            #8

            I just finished writting a bunch of verbage and an error occurd when I sent it :( And I don't think it sent...sux when that happens :) Anyways...you've helped me more often then not during my times here at CP so any time I can help back i'm more then happy. Cheers :) The word of the day is legs, let's go back to my house and spread the word ;P

            1 Reply Last reply
            0
            • R Roger Wright

              I'm not generally known as a stupid guy, but I'm really confused by this. I've been programming for 25+ years, but I've only recently tried to add C++ to my list of proficiencies, and I'm embarrassed to ask, but I'm going to anyway. I see code that uses the . operator to reference member functions as often as I see the -> operator used for what appears to me to be the same purpose. What the heck is the difference? Can someone explain to me, without the utterly useless Microsoft technical babble, what the difference in usage is? I know it's trivial to you experts, but I have a (hopefully) temporary blind spot that is blocking me from progressing on my journey of discovery on the path of C++ enlightenment. Please use small words appropriate to my small understanding for your reply... And, Thank you, for helping me to overcome this silly mental block. I'm usually good at spotting patterns, but this one has me stumped.:-O "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.

              M Offline
              M Offline
              Monty2
              wrote on last edited by
              #9

              Not knowing the difference between . and -> does not make you a stupid guy :) and Knowing the difference between . and -> does not make us any smarter either :) :-D you are OK :rose: C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do, it blows away your whole leg

              1 Reply Last reply
              0
              • R Roger Wright

                Thank you for the only marginally comprehensible answer of the lot. Frankly, pointers drive me over the edge, and make me long for the resurgence of Pascal, the only language that's made any sense in the past 20 years or so. Indirection was handy in the ASM days, and I loved using it. But it seems an absurd layer of obscurity when applied to modern languages. I really hate that part of C++. "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.

                J Offline
                J Offline
                James Pullicino
                wrote on last edited by
                #10

                The feelings you are having towards c++ are normal and understandable for a person trying to learn the language. Once you fully grasp the concepts and advantages (as well as disadvantages) of pointers you will view them in a much different light for sure.

                R 1 Reply Last reply
                0
                • A Antti Keskinen

                  Well, perhaps I'll add my knowledge to the heap. Consider the following: you have an object of some type. Let's name this type 'Roger', and let it have an attribute called 'Age' (No sarcasm intended !) Now, if in your code, you created a static object, you would use Roger MyRoger declaration. This would create a static object for you. You would then access the member variable by using MyRoger.Age = 30, for example. Let's take a step beyond and consider a pointer. See in front of yourself a 3D-space. In this space, you see the MyRoger-object standing at some point. Then, you create a pointer of type Roger using a declaration Roger* pToMyRoger = &MyRoger. Now, the pToMyRoger pointer points towards the MyRoger object, just like an extended finger. A static object is aware of two things: what it is, and where it resides. A pointer to any object is aware of two things as well: where it resides, and what is to be found in that location. For a more in-depth scenario, consider that your Roger-object (MyRoger) reserves a place for itself in memory (&MyRoger). If you issued MyRoger.Age, the compiler would get the address of MyRoger, move forwards from there to the Age variable, and retrieve it's contents. If you used a pointer, then the compiler would take the address you give to that pointer, and ASSUME that the object found from that address is the one specified by the type of the pointer (Roger*). It would then search for the Age variable again, and retrieve it. Then to the term 'dereferencing'. When you have pointer to the object Roger (pToMyRoger), you can dereference it to an OBJECT of type Roger by using the asterisk (*) operator. For example, this declaration (*pToMyRoger).Age is the same thing as MyRoger.Age. As you might guess, *pToMyRoger equals to MyRoger hence leading us to the following conclusion: the dereferencing operator (*) can sometimes be named as the 'contents-of' operator. Thus, the contents of pToMyRoger (*pToMyRoger) equals to the object MyRoger (MyRoger). Does this make sense ? In the conclusion, the '.' and the '->' operators behave similarly, but they are used in different contexts. For example, the following declarations do all the same things MyRoger.Age = (*pToMyRoger).Age = pToMyRog

                  R Offline
                  R Offline
                  Roger Wright
                  wrote on last edited by
                  #11

                  Thanks! That helps clarify things quite a bit.:-) "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.

                  1 Reply Last reply
                  0
                  • J James Pullicino

                    The feelings you are having towards c++ are normal and understandable for a person trying to learn the language. Once you fully grasp the concepts and advantages (as well as disadvantages) of pointers you will view them in a much different light for sure.

                    R Offline
                    R Offline
                    Roger Wright
                    wrote on last edited by
                    #12

                    I suppose so, though I haven't yet found any use for a pointer, nor any need to use one other than to comply with the declaration of some class or other.:sigh: "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.

                    1 Reply Last reply
                    0
                    • R Roger Wright

                      Thank you for the only marginally comprehensible answer of the lot. Frankly, pointers drive me over the edge, and make me long for the resurgence of Pascal, the only language that's made any sense in the past 20 years or so. Indirection was handy in the ASM days, and I loved using it. But it seems an absurd layer of obscurity when applied to modern languages. I really hate that part of C++. "Another day done - All targets met; all systems fully operational; all customers satisfied; all staff keen and well motivated; all pigs fed and ready to fly" - Jennie A.

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

                      Roger Wright wrote: But it seems an absurd layer of obscurity when applied to modern languages. I really hate that part of C++. Technically, pointers existed long before C++.


                      A rich person is not the one who has the most, but the one that needs the least.

                      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