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. C++ Pointer Help [Solved]

C++ Pointer Help [Solved]

Scheduled Pinned Locked Moved C / C++ / MFC
c++pythonperformancehelp
12 Posts 5 Posters 1 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.
  • T Offline
    T Offline
    Tom Moore
    wrote on last edited by
    #1

    Hi, its been a while since I've posted here :). I've come back to C++ after doing quite a lot of scripting in Python though Scripting is still a passion of mine but enough about scripting. I read an article about Memory Management in C++ the other day and some one wrote the code about borrowing a object through a reference even though it was created on the free store. I'm mixed up about the various syntaxes heres some examples of code im mixed up with. Robot.hpp

    #include <iostream>
    class Robot{
    public: void Speak(){std::cout << "Hello\n";}
    }

    main.cpp

    #include <iostream>
    #include "Robot.hpp"

    void RobotSpeak(Robot& robot); // Calls robot.Speak()
    void RobotSpeak2(Robot*& robot); // Calls robot->Speak()
    void RobotSpeak3(Robot** robot); // I imagine robot->Speak()
    void RobotSpeak4(Robot* robot); // I imagine robot->Speak() again

    int main(){ return 0; }

    When and how would you use RobotSpeak(Robot&) on a object created on the free store and when would you use the others. Many Thanks in Advance. Tom

    modified on Saturday, May 8, 2010 9:55 AM

    S C T 3 Replies Last reply
    0
    • T Tom Moore

      Hi, its been a while since I've posted here :). I've come back to C++ after doing quite a lot of scripting in Python though Scripting is still a passion of mine but enough about scripting. I read an article about Memory Management in C++ the other day and some one wrote the code about borrowing a object through a reference even though it was created on the free store. I'm mixed up about the various syntaxes heres some examples of code im mixed up with. Robot.hpp

      #include <iostream>
      class Robot{
      public: void Speak(){std::cout << "Hello\n";}
      }

      main.cpp

      #include <iostream>
      #include "Robot.hpp"

      void RobotSpeak(Robot& robot); // Calls robot.Speak()
      void RobotSpeak2(Robot*& robot); // Calls robot->Speak()
      void RobotSpeak3(Robot** robot); // I imagine robot->Speak()
      void RobotSpeak4(Robot* robot); // I imagine robot->Speak() again

      int main(){ return 0; }

      When and how would you use RobotSpeak(Robot&) on a object created on the free store and when would you use the others. Many Thanks in Advance. Tom

      modified on Saturday, May 8, 2010 9:55 AM

      S Offline
      S Offline
      Stuart Dootson
      wrote on last edited by
      #2

      Soo….something like:

      Robot* myRobot = new Robot;
      RobotSpeak(*myRobot);

      ? Oh, and BTW - the calling syntax for RobotSpeak3 would be (*robot)->Speak() The only parameter passing mechanisms I'd consider using would be those used in RobotSpeak and RobotSpeak4…although I would tend to use a const reference and declare Speak as const if it doesn't modify Robot: Robot.hpp

      #include <iostream>
      class Robot{
      public: void Speak() const {std::cout << "Hello\n";} // Use 'const' because the method doesn't alter the Robot instance
      };

      main.cpp

      void RobotSpeak(Robot const & robot) // use a const reference to Robot to indicate the function doesn't modify robot
      {
      robot.Speak();
      }

      int main()
      {
      Robot* myRobot = new Robot;
      RobotSpeak(*myRobot);
      return 0;
      }

      HTH!

      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!

      T 1 Reply Last reply
      0
      • T Tom Moore

        Hi, its been a while since I've posted here :). I've come back to C++ after doing quite a lot of scripting in Python though Scripting is still a passion of mine but enough about scripting. I read an article about Memory Management in C++ the other day and some one wrote the code about borrowing a object through a reference even though it was created on the free store. I'm mixed up about the various syntaxes heres some examples of code im mixed up with. Robot.hpp

        #include <iostream>
        class Robot{
        public: void Speak(){std::cout << "Hello\n";}
        }

        main.cpp

        #include <iostream>
        #include "Robot.hpp"

        void RobotSpeak(Robot& robot); // Calls robot.Speak()
        void RobotSpeak2(Robot*& robot); // Calls robot->Speak()
        void RobotSpeak3(Robot** robot); // I imagine robot->Speak()
        void RobotSpeak4(Robot* robot); // I imagine robot->Speak() again

        int main(){ return 0; }

        When and how would you use RobotSpeak(Robot&) on a object created on the free store and when would you use the others. Many Thanks in Advance. Tom

        modified on Saturday, May 8, 2010 9:55 AM

        C Offline
        C Offline
        CPallini
        wrote on last edited by
        #3

        I would use

        void RobotSpeak( const Robot & robot);

        with

        class Robot
        {
        public:
        void Speak() const
        {
        std::cout << "Hello\n";
        }
        };

        If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
        This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
        [My articles]

        1 Reply Last reply
        0
        • S Stuart Dootson

          Soo….something like:

          Robot* myRobot = new Robot;
          RobotSpeak(*myRobot);

          ? Oh, and BTW - the calling syntax for RobotSpeak3 would be (*robot)->Speak() The only parameter passing mechanisms I'd consider using would be those used in RobotSpeak and RobotSpeak4…although I would tend to use a const reference and declare Speak as const if it doesn't modify Robot: Robot.hpp

          #include <iostream>
          class Robot{
          public: void Speak() const {std::cout << "Hello\n";} // Use 'const' because the method doesn't alter the Robot instance
          };

          main.cpp

          void RobotSpeak(Robot const & robot) // use a const reference to Robot to indicate the function doesn't modify robot
          {
          robot.Speak();
          }

          int main()
          {
          Robot* myRobot = new Robot;
          RobotSpeak(*myRobot);
          return 0;
          }

          HTH!

          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!

          T Offline
          T Offline
          Tom Moore
          wrote on last edited by
          #4

          Thanks for replying :) . Whats the difference between passing a object by derefencing a pointed object and using a using a Reference To Pointer, and a Pointer Argument, like RobotSpeak(Robot const & robot) and RobotSpeak(Robot const *& robot) and also RobotSpeak(Robot const ** robot). Tom

          S 1 Reply Last reply
          0
          • T Tom Moore

            Thanks for replying :) . Whats the difference between passing a object by derefencing a pointed object and using a using a Reference To Pointer, and a Pointer Argument, like RobotSpeak(Robot const & robot) and RobotSpeak(Robot const *& robot) and also RobotSpeak(Robot const ** robot). Tom

            S Offline
            S Offline
            Stuart Dootson
            wrote on last edited by
            #5

            The only time I would use a reference to a pointer or a pointer to a pointer would be so I could modify the passed in pointer. And then, you need to be sure that you don't leak any memory.

            Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!

            T 1 Reply Last reply
            0
            • S Stuart Dootson

              The only time I would use a reference to a pointer or a pointer to a pointer would be so I could modify the passed in pointer. And then, you need to be sure that you don't leak any memory.

              Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!

              T Offline
              T Offline
              Tom Moore
              wrote on last edited by
              #6

              If you refer to the object by reference, it cannot by deleted, which is a good thing. Where as if you did such as RobotSpeak(const Robot*& const robot). You could delete the object in the procedure, and if the calling program tried to do any thing to object, it would fail and leak memory or worse. Thanks Again ;P Tom

              M S 2 Replies Last reply
              0
              • T Tom Moore

                If you refer to the object by reference, it cannot by deleted, which is a good thing. Where as if you did such as RobotSpeak(const Robot*& const robot). You could delete the object in the procedure, and if the calling program tried to do any thing to object, it would fail and leak memory or worse. Thanks Again ;P Tom

                M Offline
                M Offline
                molesworth
                wrote on last edited by
                #7

                Tom Moore wrote:

                Where as if you did such as RobotSpeak(const Robot*& const robot). You could delete the object in the procedure, and if the calling program tried to do any thing to object, it would fail and leak memory or worse.

                That's a very dangerous thing to do, and certainly not advisable in any API design. While you can create any level of parameter referencing you want (pointer to pointer to pointer to ...) you really have to consider why you would want to do such a thing, and what side effects you might be leaving yourself open to if you do. Even if you explicitly state things like this when describing your API, users will always do incorrect things with it. Believe me, I've written what I thought were bomb-proof systems, only to find they weren't :)

                Days spent at sea are not deducted from one's alloted span - Phoenician proverb

                T 1 Reply Last reply
                0
                • M molesworth

                  Tom Moore wrote:

                  Where as if you did such as RobotSpeak(const Robot*& const robot). You could delete the object in the procedure, and if the calling program tried to do any thing to object, it would fail and leak memory or worse.

                  That's a very dangerous thing to do, and certainly not advisable in any API design. While you can create any level of parameter referencing you want (pointer to pointer to pointer to ...) you really have to consider why you would want to do such a thing, and what side effects you might be leaving yourself open to if you do. Even if you explicitly state things like this when describing your API, users will always do incorrect things with it. Believe me, I've written what I thought were bomb-proof systems, only to find they weren't :)

                  Days spent at sea are not deducted from one's alloted span - Phoenician proverb

                  T Offline
                  T Offline
                  Tom Moore
                  wrote on last edited by
                  #8

                  Thanks, I'll be aware of that in the future. 'Bomb-proof', reminds me of the old Atari ST, whenever that crashed for whatever reason, it would print cherry bombs on the screen, we calling bombing out. That was early to mid 90's. Thanks Again. Tom

                  1 Reply Last reply
                  0
                  • T Tom Moore

                    Hi, its been a while since I've posted here :). I've come back to C++ after doing quite a lot of scripting in Python though Scripting is still a passion of mine but enough about scripting. I read an article about Memory Management in C++ the other day and some one wrote the code about borrowing a object through a reference even though it was created on the free store. I'm mixed up about the various syntaxes heres some examples of code im mixed up with. Robot.hpp

                    #include <iostream>
                    class Robot{
                    public: void Speak(){std::cout << "Hello\n";}
                    }

                    main.cpp

                    #include <iostream>
                    #include "Robot.hpp"

                    void RobotSpeak(Robot& robot); // Calls robot.Speak()
                    void RobotSpeak2(Robot*& robot); // Calls robot->Speak()
                    void RobotSpeak3(Robot** robot); // I imagine robot->Speak()
                    void RobotSpeak4(Robot* robot); // I imagine robot->Speak() again

                    int main(){ return 0; }

                    When and how would you use RobotSpeak(Robot&) on a object created on the free store and when would you use the others. Many Thanks in Advance. Tom

                    modified on Saturday, May 8, 2010 9:55 AM

                    T Offline
                    T Offline
                    Tim Craig
                    wrote on last edited by
                    #9

                    I really don't see the point in the standalone RobotSpeak function set. If you have an instance of Robot, say robot, why not just do: robot.Speak(); And if you have different types of robots saying different things, make Speak virtual and operate through base classes.

                    The wonderful thing about the Darwin Awards is that everyone wins, especially the members of the audience.

                    T 1 Reply Last reply
                    0
                    • T Tom Moore

                      If you refer to the object by reference, it cannot by deleted, which is a good thing. Where as if you did such as RobotSpeak(const Robot*& const robot). You could delete the object in the procedure, and if the calling program tried to do any thing to object, it would fail and leak memory or worse. Thanks Again ;P Tom

                      S Offline
                      S Offline
                      Stuart Dootson
                      wrote on last edited by
                      #10

                      Not with that signature - you've got a const pointer to a const object - can't call delete on that without casting the pointer... But even with a reference, you can delete it, like in this (evil, evil) code:

                      void RobotSpeak(const Robot& robot)
                      {
                      Robot* pRobot = const_cast(&robot);
                      delete pRobot;
                      }

                      Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!

                      T 1 Reply Last reply
                      0
                      • T Tim Craig

                        I really don't see the point in the standalone RobotSpeak function set. If you have an instance of Robot, say robot, why not just do: robot.Speak(); And if you have different types of robots saying different things, make Speak virtual and operate through base classes.

                        The wonderful thing about the Darwin Awards is that everyone wins, especially the members of the audience.

                        T Offline
                        T Offline
                        Tom Moore
                        wrote on last edited by
                        #11

                        Tim Craig wrote:

                        I really don't see the point in the standalone RobotSpeak function set. If you have an instance of Robot, say robot, why not just do: robot.Speak(); And if you have different types of robots saying different things, make Speak virtual and operate through base classes.

                        I know, but the point of the question wasn't about the Robot speaking, I was asking about using dynamically created objects through references.

                        1 Reply Last reply
                        0
                        • S Stuart Dootson

                          Not with that signature - you've got a const pointer to a const object - can't call delete on that without casting the pointer... But even with a reference, you can delete it, like in this (evil, evil) code:

                          void RobotSpeak(const Robot& robot)
                          {
                          Robot* pRobot = const_cast(&robot);
                          delete pRobot;
                          }

                          Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p CodeProject MVP for 2010 - who'd'a thunk it!

                          T Offline
                          T Offline
                          Tom Moore
                          wrote on last edited by
                          #12

                          Thanks, didn't realize that could be done.

                          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