473,386 Members | 1,821 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

About const

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
Jul 19 '05 #1
3 1714
"Viraj Mody" <vi***@cs.utexas.edu> wrote...
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
Jul 19 '05 #2
Viraj Mody wrote:
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

Jul 19 '05 #3
Viraj Mody <vi***@cs.utexas.edu> wrote
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.
Jul 19 '05 #4

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

Similar topics

9
by: Gianguz | last post by:
The following code compiled with gcc version 3.3.4 20040623 (Gentoo Linux 3.3.4-r1, ssp-3.3.2-2, pie-8.7.6) produce a segmentation fault and has a size of 9095 byte on my machine: #include...
5
by: Tony Johansson | last post by:
Hello experts! I have two class template below with names Array and CheckedArray. The class template CheckedArray is derived from the class template Array which is the base class This program...
0
by: David W. Fenton | last post by:
Today I was working on a hideous old app that I created a long time ago that does a lot of showing/hiding/resizing of fields on one of the forms. I had used constants to store reference values for...
2
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...
2
by: TheRomance | last post by:
i have a problem about insert integer to link list. sorry it's too long but i try many times, many ways , it's still have an error function is fix . can't change anything about function. i...
75
by: Steven T. Hatton | last post by:
No, this is not a troll, and I am not promoting Java, C-flat, D, APL, Bash, Mathematica, SML, or LISP. A college teacher recently posted to this newsgroup regarding her observation that there has...
21
by: Bo Yang | last post by:
As long as I write c++ code, I am worry about the pointer. The more the system is, the dangerous the pointer is I think. I must pass pointers erverywhere, and is there a way to ensure that every...
14
by: JoeC | last post by:
I have been writing games and I also read about good programming techniques. I tend to create large objects that do lots of things. A good example I have is a unit object. The object controls...
6
by: Peter Lee | last post by:
what's the correct behaver about the following code ? ( C++ standard ) I got a very strange result.... class MyClass { public: MyClass(const char* p) { printf("ctor p=%s\n", p);
18
by: Stephan Beal | last post by:
Hi, all! Before i ask my question, i want to clarify that my question is not about the code i will show, but about what the C Standard says should happen. A week or so ago it occurred to me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.