473,465 Members | 1,991 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Pointers and References (and References to Pointers)

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.
Jul 19 '05 #1
8 2881


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
kb******@gascad.at
Jul 19 '05 #2
Karl Heinz Buchegger <kb******@gascad.at> 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.
Jul 19 '05 #3


Roger Leigh wrote:

Karl Heinz Buchegger <kb******@gascad.at> 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
kb******@gascad.at
Jul 19 '05 #4
Karl Heinz Buchegger <kb******@gascad.at> 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.
Jul 19 '05 #5
zoe
Karl Heinz Buchegger <kb******@gascad.at> wrote in message news:<3F***************@gascad.at>...
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
Jul 19 '05 #6


zoe wrote:

Karl Heinz Buchegger <kb******@gascad.at> wrote in message news:<3F***************@gascad.at>...
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
kb******@gascad.at
Jul 19 '05 #7

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:3F***************@gascad.at...

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.
Jul 19 '05 #8


jeffc wrote:

"Karl Heinz Buchegger" <kb******@gascad.at> wrote in message
news:3F***************@gascad.at...

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
kb******@gascad.at
Jul 19 '05 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
by: Web Developer | last post by:
Hi, Can someone provide a short and concise statement(s) on the difference between pointers and references. A graphical representation (via links?) of both would be much appreciated as well. ...
4
by: cppaddict | last post by:
I understand that there are a couple differences between reference variables and pointer: 1) reference variables *must* be initialized. 2) You cannot change what a reference variable refers...
26
by: Desmond Liu | last post by:
I've read articles like Scott Meyer's EC++ (Item 22) that advocate the use of references when passing parameters. I understand the reasoning behind using references--you avoid the cost of creating...
7
by: _ed_ | last post by:
I'd like to build a class or struct composed of pointers to variables. Does this require dropping into an 'unsafe' block, or is there a trick? .... int value1 = 1234; bool value2 = false;...
4
by: Jason | last post by:
Hi, I have this old class that was used for logging error messages and I thought it would be a good exercise (for me) to convert it to .Net specifically c#. This class uses two typed pointer...
458
by: wellstone9912 | last post by:
Java programmers seem to always be whining about how confusing and overly complex C++ appears to them. I would like to introduce an explanation for this. Is it possible that Java programmers...
64
by: Zytan | last post by:
I know there are no pointers in C#, but if you do: a = b; and a and b are both arrays, they now both point to the same memory (changing one changes the other). So, it makes them seem like...
19
by: MQ | last post by:
Can someone tell me where I should use pointers and where I should use references? In his book, Stroustrup says that you should use pointers for passing arguments that are to be modified, not...
29
by: Simon Saize | last post by:
Hi - What's the point of having references (&)? Why don't we just use pointers (*)? Thanks.
8
by: darren | last post by:
Hi everybody I am a bit confused about functions that take pointers / references as parameters. Are these two functions the same? void myFunc(int& anInt); void myFunct(int* anInt(0; Would...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.