About const

V

Viraj Mody

I was just visiting some old C++ book and started thinking about something
I had seen 2 years ago - what is the maximum number of consts you can
legally use in a C++ line of code?

I came up with 5:

const int* const myClass::myFoo(const int* const arg) const {
....
}

Anyone can think of something with more 'consts' in it?

Also, while on the subject of const, wanted to verify if I have this
right:

const int* cp = &a;
--> this means that I cannot reassign cp to point to anything else.

int* const pc = &a;
--> this means that pc can point only to the address of 'const
int' variables. So I could go ahead and say:
pc = &b;
as long as b is of type 'const int'.

I have a feeling that I might have mixed up the two - please correct me if
I have.

Thanks.

-Viraj
 
V

Victor Bazarov

Viraj Mody said:
I was just visiting some old C++ book and started thinking about something
I had seen 2 years ago - what is the maximum number of consts you can
legally use in a C++ line of code?

I came up with 5:

const int* const myClass::myFoo(const int* const arg) const {
....
}

Anyone can think of something with more 'consts' in it?

Increase the number of function arguments and you will have more.

Of course, the two high-level consts (just before the function
name and just before the argument name) are superfluous.
Also, while on the subject of const, wanted to verify if I have this
right:

const int* cp = &a;
--> this means that I cannot reassign cp to point to anything else.

No. That means that the 'int' to which cp points cannot change.
'cp' is not const. *cp is. Read from right to left:

cp [is a] {pointer to} int const[ant]
int* const pc = &a;
--> this means that pc can point only to the address of 'const
int' variables. So I could go ahead and say:
pc = &b;
No.

as long as b is of type 'const int'.

The 'pc' is a constant pointer. Once initialised, it is supposed
to maintain its value.

Read from right to left:

pc [is a] const {pointer to} int
I have a feeling that I might have mixed up the two - please correct me if
I have.

Victor
 
M

Martin Magnusson

Viraj said:
const int* const myClass::myFoo(const int* const arg) const {
....
}

Anyone can think of something with more 'consts' in it?
Hmm, no, not without adding extra const arguments or putting other
expressions on the same line.
const int* cp = &a;
--> this means that I cannot reassign cp to point to anything else.
No, it means that the pointer cp can not be used to change the value of
what it points to. You can, however, make it point to something else. a
does not have to be a const int, however.
int* const pc = &a;
--> this means that pc can point only to the address of 'const
int' variables. So I could go ahead and say:
pc = &b;
as long as b is of type 'const int'.
This means the same as you described above. You can use pc to change the
value of a, but you can not change pc to point somewhere else.

/ martin
 
T

Tony Oliver

Viraj Mody said:
I was just visiting some old C++ book and started thinking about something
I had seen 2 years ago - what is the maximum number of consts you can
legally use in a C++ line of code?

I came up with 5:

const int* const myClass::myFoo(const int* const arg) const {
....
}

Anyone can think of something with more 'consts' in it?

Simply add further levels of indirection (to const) to either of the
pointers, e.g.

const int *const *const *const * myClass::myFoo( ... etc.

or increase the number of function parameters containing const
qualifiers.
Also, while on the subject of const, wanted to verify if I have this
right:

Sorry, you haven't.
const int* cp = &a;
--> this means that I cannot reassign cp to point to anything else.

This means you cannot assign to that which cp points to.
int* const pc = &a;
--> this means that pc can point only to the address of 'const
int' variables. So I could go ahead and say:

This means that you cannot reassign cp itself (but you CAN modify what
it points to).
pc = &b;
as long as b is of type 'const int'.

I have a feeling that I might have mixed up the two - please correct me if
I have.

See above.
Thanks.

-Viraj

You're welcome.

Tony.
 

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
474,145
Messages
2,570,825
Members
47,371
Latest member
Brkaa

Latest Threads

Top