Pointers and References (and References to Pointers)

Discussion in 'C++' started by Roger Leigh, Nov 12, 2003.

  1. Roger Leigh

    Roger Leigh Guest

    Hello again,

    I have a bit of a strange problem here. This code works OK:

    class foo
    {
    public:
    foo(int **ref):
    m_ref(ref)
    {}
    private:
    int **m_ref;
    };

    int main()
    {
    int i = 1;
    int *iptr = &i;
    foo bar(&iptr);
    return 0;
    }

    but this code does not:

    class foo
    {
    public:
    foo(int& *ref):
    m_ref(ref)
    {}
    private:
    int& *m_ref;
    };

    int main()
    {
    int i = 1;
    foo bar(&i);
    return 0;
    }

    I get this error when I compile it:

    $ g++ -o test test.cc
    test.cc:4: error: cannot declare pointers to references
    test.cc:8: error: cannot declare pointers to references

    This is just a testcase. From another compiler error, it looks like
    the compiler (GCC) thinks that the prototype for foo::foo() is
    actually foo::foo(int*), not foo::foo(int&*).


    The reason I want this behaviour is as follows: I have a database
    "transaction object" representing a transaction. It is allocated on
    the heap with new, and passed to the constructors of several classes
    which are instantiated by the parent class (and so on, recursively).
    All the classes are part of the same database transaction, and so need
    access to the same transaction object.

    Now, I can't pass the object by value as a reference: it's perfectly
    valid for it to be NULL, which causes transactions to be dynamically
    created as required, rather than using the user-supplied one. It's
    also valid for a child to delete the transaction object and/or create
    a new one, in which case I want the pointer in the parent and other
    children to be updated. This won't happen if I just pass a plain
    pointer, since the pointer is passed by value. I could implement this
    as a pointer to a pointer (see top example), but it seemed cleaner as
    a reference to a pointer, since all the children could treat it as a
    plain pointer, without having to dereference it twice.


    Could anyone point out what I am misunderstanding here? Is what I
    want to do at all possible?

    If I can have a reference to a datatype or structure, why shouldn't I
    have a reference to a pointer, which is also a datatype?


    Thanks,
    Roger

    --
    Roger Leigh

    Printing on GNU/Linux? http://gimp-print.sourceforge.net/
    GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
    Roger Leigh, Nov 12, 2003
    #1
    1. Advertising

  2. Roger Leigh wrote:
    >

    [snip]
    >
    > Could anyone point out what I am misunderstanding here? Is what I
    > want to do at all possible?


    References don't have addresses. A reference is just another name for
    a variable. You can't take the address of reference.

    >
    > If I can have a reference to a datatype or structure, why shouldn't I
    > have a reference to a pointer, which is also a datatype?


    You didn't declare a reference to a pointer.
    You tried to declare a pointer to a reference.

    int& * pI; // pI is a pointer to a reference -- illegal
    int* & pJ; // pJ is a reference to a pointer -- legal.



    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Nov 13, 2003
    #2
    1. Advertising

  3. Roger Leigh

    Roger Leigh Guest

    Karl Heinz Buchegger <> writes:

    > Roger Leigh wrote:


    > References don't have addresses. A reference is just another name for
    > a variable. You can't take the address of reference.
    >
    >> If I can have a reference to a datatype or structure, why shouldn't I
    >> have a reference to a pointer, which is also a datatype?

    >
    > You didn't declare a reference to a pointer.
    > You tried to declare a pointer to a reference.
    >
    > int& * pI; // pI is a pointer to a reference -- illegal
    > int* & pJ; // pJ is a reference to a pointer -- legal.


    Ahh, it all makes sense now! Thanks!

    Now I have this all working, is it possible to specify a default
    parameter in such a method. For example (this doesn't work):

    class foo
    {
    foo(refptr*& transaction = *(refptr*)(NULL));
    ...
    };

    You always need to pass a refptr* as the reference. Is it possible to
    specify a default argument of NULL as the value of the referent? That
    is, a "refptr*" object is instantiated, assigned the NULL value and
    then passed by reference? i.e. I don't want to pass a NULL reference,
    but I want the value of the pointer passed as the reference to be NULL
    by default.


    Thanks again,
    Roger

    --
    Roger Leigh

    Printing on GNU/Linux? http://gimp-print.sourceforge.net/
    GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
    Roger Leigh, Nov 13, 2003
    #3
  4. Roger Leigh wrote:
    >
    > Karl Heinz Buchegger <> writes:
    >
    > > Roger Leigh wrote:

    >
    > > References don't have addresses. A reference is just another name for
    > > a variable. You can't take the address of reference.
    > >
    > >> If I can have a reference to a datatype or structure, why shouldn't I
    > >> have a reference to a pointer, which is also a datatype?

    > >
    > > You didn't declare a reference to a pointer.
    > > You tried to declare a pointer to a reference.
    > >
    > > int& * pI; // pI is a pointer to a reference -- illegal
    > > int* & pJ; // pJ is a reference to a pointer -- legal.

    >
    > Ahh, it all makes sense now! Thanks!
    >
    > Now I have this all working, is it possible to specify a default
    > parameter in such a method. For example (this doesn't work):
    >
    > class foo
    > {
    > foo(refptr*& transaction = *(refptr*)(NULL));
    > ...
    > };
    >
    > You always need to pass a refptr* as the reference. Is it possible to
    > specify a default argument of NULL as the value of the referent?


    You need a pointer variable which has a value of NULL. You need this
    variable in order to be able to take a reference from it.

    static refptr* NullRef = NULL;

    class foo
    {
    foo( refptr*& transaction = NullRef );

    > That
    > is, a "refptr*" object is instantiated, assigned the NULL value and
    > then passed by reference? i.e. I don't want to pass a NULL reference,
    > but I want the value of the pointer passed as the reference to be NULL
    > by default.


    From what I have read and seen up to now, I am not convinced that you
    want a reference at all, but I am convonced that you have confused
    yourself with the reference. Why not simply store a passed pointer as
    pointer?

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Nov 13, 2003
    #4
  5. Roger Leigh

    Roger Leigh Guest

    Karl Heinz Buchegger <> writes:

    > Roger Leigh wrote:
    >> You always need to pass a refptr* as the reference. Is it possible to
    >> specify a default argument of NULL as the value of the referent?

    >
    > You need a pointer variable which has a value of NULL. You need this
    > variable in order to be able to take a reference from it.
    >
    > static refptr* NullRef = NULL;
    >
    > class foo
    > {
    > foo( refptr*& transaction = NullRef );


    I understand that I can do this, but I was hoping I could have a
    refptr* instantiated, assigned a NULL value and then passed as the
    default parameter, saving me from doing it "manually".

    >> That
    >> is, a "refptr*" object is instantiated, assigned the NULL value and
    >> then passed by reference? i.e. I don't want to pass a NULL reference,
    >> but I want the value of the pointer passed as the reference to be NULL
    >> by default.

    >
    > From what I have read and seen up to now, I am not convinced that you
    > want a reference at all, but I am convonced that you have confused
    > yourself with the reference. Why not simply store a passed pointer as
    > pointer?


    Because I really need a pointer to a pointer, such that if I change
    the [pointed-to] pointer its state will be reflected in the "parent"
    class, since the object it points to reflects shared state needed by
    (potentially) several objects. I don't strictly need a reference to a
    pointer, but it's nicer than a pointer to a pointer, because I can't
    accidentally change or dereference the wrong one.

    It's nicer to write

    void foo(refptr*& ptr)
    {
    if (ptr)
    ptr->foo();
    }

    than

    void foo(refptr **ptr)
    {
    if (*ptr)
    (*ptr)->foo();
    }

    Neither approach is all that nice, really, so I think I'll wrap the
    pointer in a class, and pass that by reference instead. This should
    be far cleaner and easier to understand, and I can keep additional
    stuff in the class (database connection and transaction objects, and
    potentially other shared state describing what's going on with the
    database).


    Thanks for your help!
    Roger

    --
    Roger Leigh

    Printing on GNU/Linux? http://gimp-print.sourceforge.net/
    GPG Public Key: 0x25BFB848. Please sign and encrypt your mail.
    Roger Leigh, Nov 13, 2003
    #5
  6. Roger Leigh

    zoe Guest

    Karl Heinz Buchegger <> wrote in message news:<>...
    > Roger Leigh wrote:
    > >

    > [snip]
    > >
    > > Could anyone point out what I am misunderstanding here? Is what I
    > > want to do at all possible?

    >
    > References don't have addresses. A reference is just another name for
    > a variable. You can't take the address of reference.



    Now i am confused!
    FAQ section 8.6 tells me:

    Unlike a pointer, once a reference is bound to an object, it can not
    be "reseated" to another object. The reference itself isn't an object
    (it has no identity; taking the address of a reference gives you the
    address of the referent; remember: the reference is its referent).


    Zoe
    zoe, Nov 14, 2003
    #6
  7. zoe wrote:
    >
    > Karl Heinz Buchegger <> wrote in message news:<>...
    > > Roger Leigh wrote:
    > > >

    > > [snip]
    > > >
    > > > Could anyone point out what I am misunderstanding here? Is what I
    > > > want to do at all possible?

    > >
    > > References don't have addresses. A reference is just another name for
    > > a variable. You can't take the address of reference.

    >
    > Now i am confused!
    > FAQ section 8.6 tells me:
    >
    > Unlike a pointer, once a reference is bound to an object, it can not
    > be "reseated" to another object. The reference itself isn't an object
    > (it has no identity; taking the address of a reference gives you the
    > address of the referent; remember: the reference is its referent).
    >


    What's confusing?

    'taking the address of a reference gives you the address of the referent'

    in other words: you can't take the address of a reference.
    Whatever you try to do with the reference, it will be applied to the thing
    the reference stands for. In this way a reference is just another name for
    an otherwise existing object.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Nov 14, 2003
    #7
  8. Roger Leigh

    jeffc Guest

    "Karl Heinz Buchegger" <> wrote in message
    news:...
    >
    > What's confusing?
    >
    > 'taking the address of a reference gives you the address of the referent'
    >
    > in other words: you can't take the address of a reference.


    That is hardly "in other words". The first part implies you can take the
    address of a reference. The second part directly contradicts that. It's
    obvious why there's confusion.
    jeffc, Nov 14, 2003
    #8
  9. jeffc wrote:
    >
    > "Karl Heinz Buchegger" <> wrote in message
    > news:...
    > >
    > > What's confusing?
    > >
    > > 'taking the address of a reference gives you the address of the referent'
    > >
    > > in other words: you can't take the address of a reference.

    >
    > That is hardly "in other words". The first part implies you can take the
    > address of a reference.


    Aehm. No.
    It tells: if you intend to take the address of a reference, you really get the
    address to what the reference stands for.

    The second sentence says: a reference by itself doesn't have an address hence
    you can't take the address of the *reference*.

    Of course you can apply the 'address-of' operator to a reference. But doing
    this will never reveal the address where the information is stored to what
    other object the reference refers to. The reference itself is completely
    transparent to the C++ program, it behaves in all aspects as if it doesn't
    exist at all and as if you would apply all operations directly on the object
    the reference stands for.

    --
    Karl Heinz Buchegger
    Karl Heinz Buchegger, Nov 17, 2003
    #9
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    3
    Views:
    438
    Victor Bazarov
    Nov 10, 2004
  2. DanielEKFA
    Replies:
    8
    Views:
    591
    DanielEKFA
    May 16, 2005
  3. Replies:
    8
    Views:
    697
    Bruno Desthuilliers
    Dec 12, 2006
  4. Lars Willich
    Replies:
    13
    Views:
    823
    Ian Shef
    Oct 23, 2007
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    657
Loading...

Share This Page