Pointers and References (and References to Pointers)

R

Roger Leigh

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
 
K

Karl Heinz Buchegger

Roger said:
[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.
 
R

Roger Leigh

Karl Heinz Buchegger said:
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.


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
 
K

Karl Heinz Buchegger

Roger said:
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?
 
R

Roger Leigh

Karl Heinz Buchegger said:
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".
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
 
Z

zoe

Karl Heinz Buchegger said:
Roger said:
[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
 
K

Karl Heinz Buchegger

zoe said:
Karl Heinz Buchegger said:
Roger said:
[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.
 
J

jeffc

Karl Heinz Buchegger said:
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.
 
K

Karl Heinz Buchegger

jeffc said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top