473,574 Members | 3,182 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Beginner's question: What is "const int * &"?

Can anyone clearly explain the difference between constant reference to
pointers and reference to constant pointers?

What is const int * & ?
Is it a constant reference to a pointer to an integer? Or
Is it a reference to a pointer to a constant integer?
What is being constant in this case? The pointer or the integer being pointed?

How about int * const & ?
Is this a reference to a constant pointer to an integer? Or
Is this a constant reference to a pointer to an integer
What is being constant in this case?

What about int const * & ?
Strangely enough, this compiles. What is this?
And what is being constant in this case?

How about the following cases?

const int * const &
const int const * & g++ says "duplicate constant"

I would very much appreciate clear explanation on these. Thank you in advance.

From very confused.
Jul 19 '05 #1
2 16228
For a good text on good usage of const, look at:

http://users.utu.fi/~sisasa/oasis/cp...rrectness.html

/ Martin
Jul 19 '05 #2
co******@yahoo. co.uk (CoolPint) wrote in message news:<15******* *************** ****@posting.go ogle.com>...

Read each token in the declaration from right to left.
Can anyone clearly explain the difference between constant reference to
pointers and reference to constant pointers?

What is const int * & ?
Is it a constant reference to a pointer to an integer? Or
Is it a reference to a pointer to a constant integer?
What is being constant in this case? The pointer or the integer being pointed?
Lets make it a proper declaration.
const int * & i;

From right to left: "i is a reference to a pointer to a constant int".
Except I've broken my right to left rule here by reading the tokens in
the order

&
*
const
int

See below for the explanation, after your third example.
How about int * const & ?
Is this a reference to a constant pointer to an integer? Or
Is this a constant reference to a pointer to an integer
What is being constant in this case?
int * const & i;
"i is a reference to a constant pointer to an int". No problems ;-)

What about int const * & ?
Strangely enough, this compiles. What is this?
And what is being constant in this case?
int const * & i;
Right to left again: "i is a reference to a pointer to a constant
int". Now that's exactly the same as the first one. What I think is
confusing you is this - leaving pointers and references out of it, are
you aware that

const int i;
and
int const i;

both mean the same thing? The only difference is that the second one
makes sense reading from right to left ("i is a constant int"). For
this reason some people prefer to always put the type before the
const. Whenever const and a type name (int in your examples) appear
next to each other they can appear in either order. So

const int * & i;
int const * & i;

both mean "i is a reference to a pointer to a constant int".
How about the following cases?

const int * const &
const int * const & i;
"i is a reference to a constant pointer to a constant integer".
This could also be written
int const * const & i;
which reads better right to left.
const int const * & g++ says "duplicate constant"
const int const * & i;
"i is a reference to a pointer to a ... errrm constant int constant
???" What's a constant int constant? Remove one of the consts and you
have either
int const * & i;
or
const int * & i;
which we've seen above both mean the same thing. Since both occurances
of const in your declaration would do the same thing if the other
const wasn't there, you are getting a message about duplicate const.
I would very much appreciate clear explanation on these. Thank you in advance.

From very confused.


I hope you are less confused now :)

GJD
Jul 19 '05 #3

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

Similar topics

6
4359
by: hoox2 | last post by:
void push_front(Node const*& head, int data); Can someone tell me what it means for "const*&"? A reference or a pointer?
11
3609
by: BoonHead, The Lost Philosopher | last post by:
I think the .NET framework is great! It's nice, clean and logical; in contradiction to the old Microsoft. It only saddens me that the new Microsoft still doesn't under stand there own rules when it comes to file paths. A lot of Microsoft installers for example, and also installers of other companies, do not work because they handle...
12
2587
by: zealotcat | last post by:
template <class T> inline T const& max (T const& a, T const& b) { // if a < b then use b else use a return a<b?b:a; } thanks very much!!
3
1845
by: Alexander Farber | last post by:
Hi, does anyone have an idea, why do I get the following error (I have to use g++296 on RedHat Linux as compiler): In file included from r_dir.cpp:9: r_obey.h:262: declaration of `const AreaSet &CObeyFile::AreaSet () const' r_areaset.h:197: changes meaning of `AreaSet' from `class AreaSet'
4
14774
by: barney | last post by:
Hello, I' m using .NET System.Xml.XmlDOcument. When I do the following: XmlDocument xml = new XmlDocument(); xml.Load("blah"); .... xml.Save("blub"); I've got the problem that the following expression: .... snip ...
5
3426
by: martin | last post by:
Hi, I would be extremly grateful for some help on producing an xml fragemt. The fragment that I wish to produce should look like this <Addresses> <Address>&qout;Somebody's Name&quot; &lt;me@mydomain.com&gt;</Address> </Addresses>
2
1900
by: Rouben Rostamian | last post by:
The main() function in the following code defines an m by n matrix, assigns value(s) to its elements, then passes the matrix to function foo(). For whatever it's worth, I have declared foo() so as to make it treat its first argument as a "read-only" object, that is, foo() can read but not alter the matrix. I have a problem, however, with...
14
5916
by: Arne | last post by:
A lot of Firefox users I know, says they have problems with validation where the ampersand sign has to be written as &amp; to be valid. I don't have Firefox my self and don't wont to install it only because of this, so I hope some of you gurus can enlighten me with this :) In what circumstances can the "&amp;" in the source code be involuntary...
6
5574
by: Darin Johnson | last post by:
I keep running across that I'm maintaining that likes to define function parameters as "const char &" or "const int &", etc. Ie, constant reference parameters to a primitive type. This is for normal functions, not operators. I keep changing these to just have the plain old type, which is more efficient (I'm using embedded systems) and less...
0
7841
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...
0
7758
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...
0
8100
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. ...
1
7856
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...
0
6509
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...
0
3773
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...
0
3793
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2270
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
0
1099
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.