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
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Passing a reference to a pointer

Passing a reference to a pointer

Scheduled Pinned Locked Moved C / C++ / MFC
c++question
4 Posts 4 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.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    One of my colleagues asked me to have a look at some of his code with him. In it he had a method that performed a lookup of an stl collection. If the key was found he retured pointers to two objects through references passed into the method. bool FindSomethingAndSetThesePointers(const string& key, Class1 *&obj1, Class2 *&obj2); I sugested that it was pretty unusual way of doing things and looked a little "smelly". When he pressed me on it I found it hard to give a good reason not to do things this way but I still feel a bit uneasy about it. What do you guys think? ps I suggested that the reference be made a const and we both scratched our heads for a bit thinking where the const key word should go

    A 1 Reply Last reply
    0
    • L Lost User

      One of my colleagues asked me to have a look at some of his code with him. In it he had a method that performed a lookup of an stl collection. If the key was found he retured pointers to two objects through references passed into the method. bool FindSomethingAndSetThesePointers(const string& key, Class1 *&obj1, Class2 *&obj2); I sugested that it was pretty unusual way of doing things and looked a little "smelly". When he pressed me on it I found it hard to give a good reason not to do things this way but I still feel a bit uneasy about it. What do you guys think? ps I suggested that the reference be made a const and we both scratched our heads for a bit thinking where the const key word should go

      A Offline
      A Offline
      Arman S
      wrote on last edited by
      #2

      Josh Gray wrote:

      One of my colleagues asked me to have a look at some of his code with him. In it he had a method that performed a lookup of an stl collection. If the key was found he retured pointers to two objects through references passed into the method. bool FindSomethingAndSetThesePointers(const string& key, Class1 *&obj1, Class2 *&obj2); I sugested that it was pretty unusual way of doing things and looked a little "smelly". When he pressed me on it I found it hard to give a good reason not to do things this way but I still feel a bit uneasy about it. What do you guys think?

      I don't think this is 'smelly'. Usually, we do such things when it is really needed. In this case, you should decide what you want to do with the pointers returned. If your collection holds pointers [to some objects], after returned these two pointers, you are able to modify the values of them and thus directly affect the pointers inside the collection. For example, FindSomethingAndSetThesePointers(""somejey", p, q); p = NULL; this will set the corresponding element of the collection to NULL. So you were able to directly change the elements of the collection. Anyway, if you do not need such freedom of changing the collection outside the FindSomethingAndSetThesePointers, then no need to Class*&, a simple Class* will do. If your collection holds objects (not pointers to them), you may return either pointer to them, or reference to them, or even a copy of them (if you think it is appropriate). bool FindSomethingAndSetThesePointers(const string& key, Class1 &obj1, Class2 &obj2); Sometimes, choosing between Class *' and Class &' is either a matter of taste or a convention.

      -- ===== Arman

      C C 2 Replies Last reply
      0
      • A Arman S

        Josh Gray wrote:

        One of my colleagues asked me to have a look at some of his code with him. In it he had a method that performed a lookup of an stl collection. If the key was found he retured pointers to two objects through references passed into the method. bool FindSomethingAndSetThesePointers(const string& key, Class1 *&obj1, Class2 *&obj2); I sugested that it was pretty unusual way of doing things and looked a little "smelly". When he pressed me on it I found it hard to give a good reason not to do things this way but I still feel a bit uneasy about it. What do you guys think?

        I don't think this is 'smelly'. Usually, we do such things when it is really needed. In this case, you should decide what you want to do with the pointers returned. If your collection holds pointers [to some objects], after returned these two pointers, you are able to modify the values of them and thus directly affect the pointers inside the collection. For example, FindSomethingAndSetThesePointers(""somejey", p, q); p = NULL; this will set the corresponding element of the collection to NULL. So you were able to directly change the elements of the collection. Anyway, if you do not need such freedom of changing the collection outside the FindSomethingAndSetThesePointers, then no need to Class*&, a simple Class* will do. If your collection holds objects (not pointers to them), you may return either pointer to them, or reference to them, or even a copy of them (if you think it is appropriate). bool FindSomethingAndSetThesePointers(const string& key, Class1 &obj1, Class2 &obj2); Sometimes, choosing between Class *' and Class &' is either a matter of taste or a convention.

        -- ===== Arman

        C Offline
        C Offline
        Cedric Moonen
        wrote on last edited by
        #3

        Arman Z. Sahakyan wrote:

        If your collection holds objects (not pointers to them), you may return either pointer to them, or reference to them, or even a copy of them (if you think it is appropriate).

        I don't think this is a good idea: if the container is reallocated, your pointer will become invalid.


        Cédric Moonen Software developer
        Charting control [v1.2]

        1 Reply Last reply
        0
        • A Arman S

          Josh Gray wrote:

          One of my colleagues asked me to have a look at some of his code with him. In it he had a method that performed a lookup of an stl collection. If the key was found he retured pointers to two objects through references passed into the method. bool FindSomethingAndSetThesePointers(const string& key, Class1 *&obj1, Class2 *&obj2); I sugested that it was pretty unusual way of doing things and looked a little "smelly". When he pressed me on it I found it hard to give a good reason not to do things this way but I still feel a bit uneasy about it. What do you guys think?

          I don't think this is 'smelly'. Usually, we do such things when it is really needed. In this case, you should decide what you want to do with the pointers returned. If your collection holds pointers [to some objects], after returned these two pointers, you are able to modify the values of them and thus directly affect the pointers inside the collection. For example, FindSomethingAndSetThesePointers(""somejey", p, q); p = NULL; this will set the corresponding element of the collection to NULL. So you were able to directly change the elements of the collection. Anyway, if you do not need such freedom of changing the collection outside the FindSomethingAndSetThesePointers, then no need to Class*&, a simple Class* will do. If your collection holds objects (not pointers to them), you may return either pointer to them, or reference to them, or even a copy of them (if you think it is appropriate). bool FindSomethingAndSetThesePointers(const string& key, Class1 &obj1, Class2 &obj2); Sometimes, choosing between Class *' and Class &' is either a matter of taste or a convention.

          -- ===== Arman

          C Offline
          C Offline
          codeII
          wrote on last edited by
          #4

          :doh:This technique can be used to assign/reassign a pointer within a function. Another way of doing this is a pointer to a pointer **. exampl. Void MyAllocFunction( TYPE*& pData ) { delete [] pData; pData = new TYPE[ x ]; } TYPE* pData = NULL; MyAllocFunction( pData ); delete [] pData; From these methods I prefer a reference to a pointer for readability and error safety reasons. Preferably a function which allocates a pointer has to destroy it; this keeps it clear what function is responsible for destruction. From this point of view you have to provide using both of these techniques.

          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