473,623 Members | 2,433 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

rvalues and constant reference to a pointer ?

I know this is something fundamental and I ought to have known it, but
somehow this seems to be confusing me a lot.

Fundamentally, rvalues and/or temporaries can be bound only to constant
references going by the const guidelines.

Taking that into consideration, how does one get a constant reference
to a pointer.

class A
{};

1> A* foo(A* & ptr){ } // non-const reference to a pointer

2> A* foo( const A* & ptr) { } //reference to a pointer to const A

3> A* foo(A* const & ptr) { } // reference to a constant pointer to A

Now when I make a call like

foo( new A()); or
foo( &A());

The first one doesnt work as it is a non const reference and neither
does the second which is a non const reference to a pointer to const
object, which makes sense.

It does work with the third declaration which again is "ONLY a
reference to a const pointer"...( and this is what I have been using
all along when I have to do something like this).

Now my question is, logically speaking rvalues/temporaries can be bound
only to const references...so the two calls that I have to foo,
shouldnt work with the 3 declaration...b ut it does work, which I have
just blindly assumed until now and used it whenever I run into such
situations.

But then how does one realy have a constant reference to a pointer in
this case ? const usually binds to the left(unless there is nothing to
the left, so then it binds to the right)...so in that case it would be
the same as declaration no 3.

which would mean that in reality, there is nothing as a const reference
to a pointer.

Thanks.

Jul 23 '05 #1
6 1835
Me
> 1> A* foo(A* & ptr){ } // non-const reference to a pointer
2> A* foo( const A* & ptr) { } //reference to a pointer to const A
3> A* foo(A* const & ptr) { } // reference to a constant pointer to A

Now when I make a call like

foo( new A()); or
foo( &A());

The first one doesnt work as it is a non const reference and neither
does the second which is a non const reference to a pointer to const
object, which makes sense.

It does work with the third declaration which again is "ONLY a
reference to a const pointer"...( and this is what I have been using
all along when I have to do something like this).

Now my question is, logically speaking rvalues/temporaries can be bound
only to const references...so the two calls that I have to foo,
shouldnt work with the 3 declaration...b ut it does work, which I have
just blindly assumed until now and used it whenever I run into such
situations.


Rewrite your examples using typedefs:

typedef A * Ap;
typedef const A * C_Ap;

1> Ap foo(Ap &ptr);
2> Ap foo(C_Ap &ptr);
3> Ap foo(const Ap &ptr);

Still confused?

4> Ap foo(const C_Ap &ptr);

also works. Get yourself a copy of the standard and read section 4.4
about const safety to see why (it's hard to see why here, but if you
write it out without the typedefs, it's easier).

Jul 23 '05 #2
am******@gmail. com wrote:

Fundamentally, rvalues and/or temporaries can be bound only to constant
references going by the const guidelines.

Taking that into consideration, how does one get a constant reference
to a pointer.

3> A* foo(A* const & ptr) { } // reference to a constant pointer to A


Actually this is a const reference to a pointer to A.
Since the reference is const, it's irrelevant whether or not
the pointer it's referring to is const (because the reference
cannot be used to modify it, in either case).

This explains why your code samples work with this version,
and makes the rest of your message moot.

Jul 23 '05 #3
Me
> > 3> A* foo(A* const & ptr) { } // reference to a constant pointer to A

Actually this is a const reference to a pointer to A.
Since the reference is const, it's irrelevant whether or not
the pointer it's referring to is const (because the reference
cannot be used to modify it, in either case).


His comment is right. A * & const would be a const reference to a
pointer to A (which is meaningless:
http://www.parashift.com/c++-faq-lit...html#faq-18.7).

Jul 23 '05 #4
Old Wolf wrote:
am******@gmail. com wrote:

Fundamentally, rvalues and/or temporaries can be bound only to constant
references going by the const guidelines.

Taking that into consideration, how does one get a constant reference
to a pointer.

3> A* foo(A* const & ptr) { } // reference to a constant pointer to A


Actually this is a const reference to a pointer to A.


There is no such thing as a "const reference". The above is a reference to
"A* const", just like "A* const *" would be a (non-const) pointer to
"A* const".

Jul 23 '05 #5
Rolf Magnus wrote:
Old Wolf wrote:
am******@gmail. com wrote:

Fundamentally, rvalues and/or temporaries can be bound only to constant
references going by the const guidelines.

Taking that into consideration, how does one get a constant reference
to a pointer.

3> A* foo(A* const & ptr) { } // reference to a constant pointer to A
Actually this is a const reference to a pointer to A.


There is no such thing as a "const reference".


A 'const reference' is a reference where the thing it's
referring to, can't be modified via the reference.
(I don't know if this term is in the Standard, but it seems
fairly common practice for describing a type such as T const & )
The above is a reference to "A* const"


In this case:

A *p;
A * const &q = p;

q refers to p, and p is an A * (not an A * const).
However, p looks like an A * const when viewed through q.

The OP did ask, "how does one get a constant reference
to a pointer". (to which the answer is, his #3).

Jul 23 '05 #6
Old Wolf wrote:
Rolf Magnus wrote:
Old Wolf wrote:
am******@gmail. com wrote:

Fundamentally, rvalues and/or temporaries can be bound only to constant
references going by the const guidelines.

Taking that into consideration, how does one get a constant reference
to a pointer.

3> A* foo(A* const & ptr) { } // reference to a constant pointer to A

Actually this is a const reference to a pointer to A.


There is no such thing as a "const reference".


A 'const reference' is a reference where the thing it's
referring to, can't be modified via the reference.
(I don't know if this term is in the Standard, but it seems
fairly common practice for describing a type such as T const & )


I use it myself sometimes, because it's a bit shorter than "reference to
const".
The above is a reference to "A* const"


In this case:

A *p;
A * const &q = p;

q refers to p, and p is an A * (not an A * const).
However, p looks like an A * const when viewed through q.


And what about this case:

A *p;
A * const * q = &p;

p looks like an A * const when viewed through the dereferenced q. Still I
wouldn't call p a const pointer, since it isn't const. It's a pointer to
const, even though the pointed-to object is actually not a constant.

Jul 23 '05 #7

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

Similar topics

1
9981
by: Kent | last post by:
Hi! I am working with an obscure graphics library in school as a university C++ programming project. The graphics library has a class kalled Bitmap wich you use to draw graphics (you can "stamp it down" on the screen, double buffering and such is all within the lib). This is how it works now (i think its a bad design): In a class i created to represent "monsters" in a game i have the following
12
2742
by: Kristian Bisgaard Lassen | last post by:
Hi, How do I parameterize a template by a a allocated array of integers, which is declared static and constant, so I can make compile time optimizations depending on the content of the array. The way I have written my code makes g++ complain about it not being static. BTW I am new to the C++ language and also the templates it provides. Here is an example of what I want to do
11
404
by: Steven T. Hatton | last post by:
This is from the draft of the previous version of the Standard: http://www.kuzbass.ru:8086/docs/isocpp/expr.html 2- A literal is a primary expression. Its type depends on its form (lex.literal). A string literal is an *lvalue*; all other literals are *rvalues*. -4- The operator :: followed by an identifier, a qualified-id, or an operator-function-id is a primary-expression. Its type is specified by the
20
2071
by: CoolPint | last post by:
While I was reading about const_cast, I got curious and wanted to know if I could modify a constant variable through a pointer which has been "const_cast"ed. Since the pointer would be pointing to the constant variable and if I changed the value through the pointer, the constant variable should have been modified. Well, that's what I thought until I wrote the following and run it. #include <iostream> using std::cout; using std::endl;
11
5280
by: lovecreatesbeauty | last post by:
Hello experts, Is const_cast only applied to pointers or references? If I have a constant object, then how can I remove constant attribute from it? #include <vector> #include <string> using namespace std;
11
2363
by: Mantorok Redgormor | last post by:
Is const really constant? And on an OT note: how can I post with a modified e-mail address so I don't get so much spam?
3
2718
by: ramasubramanian.rahul | last post by:
i was reading soemthing about Lvalues Rvalues and modifiable Lvalues.... i am confused now... like for eg int *y , x ; y = &x ; in the second is x a lvalue or rvalue any pointers on some good reading on lvalues and rvalues on the web ?? thanks in advance Kind Regard
12
1287
by: Bob Altman | last post by:
Hi all, I want to call a function that takes a void* argument and pass it the address of a constant. Is there a way to do this without explicitly defining the constant and then passing the address of the constant? In other words, what I *don't* want to do is this: const int myInt = 5; MyFunc(&myInt);
6
1777
by: Ruben | last post by:
re-reviewing the chapter in Lippan and Lajoie C++ Primer the have a section on references that I don't understand and just makes e shake my head believing that any behavior is possible with references and that they can never be understood. Maybe someone else can rephrase this so that I understand the defined behavior. cont int ival = 1024; //error: requires a const reference int *&pr_ref = &ival; //<<== this seems obvious as a...
0
8227
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
8165
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,...
1
8326
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8469
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7150
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
6106
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
5561
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
4074
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...
0
4164
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.