473,804 Members | 3,481 Online
Bytes | Software Development & Data Engineering Community
+ 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
"transactio n 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 misunderstandin g 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 2896


Roger Leigh wrote:
[snip]
Could anyone point out what I am misunderstandin g 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******@gasca d.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******@gasca d.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******@gasca d.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******@gasca d.at> wrote in message news:<3F******* ********@gascad .at>...
Roger Leigh wrote:

[snip]

Could anyone point out what I am misunderstandin g 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******@gasca d.at> wrote in message news:<3F******* ********@gascad .at>...
Roger Leigh wrote:

[snip]

Could anyone point out what I am misunderstandin g 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******@gasca d.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******@gasca d.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
14042
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. WD
4
4960
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 to. My question is: In what situations is it better to use a reference variable over a pointer? Also, are there other differences besides the ones I listed above?
26
2474
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 a local object when you pass an object by reference. But why use a reference? Is there any inherent advantage of using a reference over using a pointer? My workplace discourages the use of pointers when it comes to parameter passing. They...
7
1890
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; string value3 = "Hello Detroit"; public class x {
4
1146
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 lists to process log messages. One is the input queue for incoming messages and the other is the processing queue for processing the messages. The thread that does the processing will process all the objects on the procesing queue and then swap the...
458
21535
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 simply aren't smart enough to understand C++? This is not merely a whimsical hypothesis. Given my experience with Java programmers --- the code they write and the conversations they have --- Occam's Razor points to this explanation. For example,...
64
3442
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 pointers. Can someone please explain why? thanks. Zytan
19
1996
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 references. Yet, in an interview, he laments the overuse of pointers and such in code. It seems contradictory to me. Why should we not use references instead of pointers? Can someone give me an idea of best practices in this respect? regards,...
29
1521
by: Simon Saize | last post by:
Hi - What's the point of having references (&)? Why don't we just use pointers (*)? Thanks.
8
1186
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 the difference be that, in the first function, i would us anInt
0
9710
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9589
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10593
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9163
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7626
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6858
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5527
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4304
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 we have to send another system
2
3830
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.