| View previous topic :: View next topic |
| Author |
Message |
Last Timer Guest
|
Posted: Sun Jan 30, 2005 7:31 pm Post subject: Elementary question on const |
|
|
I encountered the following code in Bruce Eckel's online book. Can you
please clarify what "const char* const data;" means? Thanks
//: C01:MyError.cpp {RunByHand}
class MyError {
const char* const data;
public:
MyError(const char* const msg = 0) : data(msg) {}
};
void f() {
// Here we "throw" an exception object:
throw MyError("something bad happened");
}
int main() {
// As you'll see shortly, we'll want a "try block" here:
f();
} ///:~
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Sun Jan 30, 2005 7:51 pm Post subject: Re: Elementary question on const |
|
|
"Last Timer" <dakshing64 (AT) yahoo (DOT) com> wrote
| Quote: | I encountered the following code in Bruce Eckel's online book. Can you
please clarify what "const char* const data;" means?
|
Doesn't the book explain it?
| Quote: | //: C01:MyError.cpp {RunByHand}
class MyError {
const char* const data;
|
This means that the data member 'data' is a const
pointer to a const type 'char' object. I.e. neither
the pointer nor what it points to may be modified.
(If you try, your compiler should complain).
-Mike
|
|
| Back to top |
|
 |
David Harmon Guest
|
Posted: Sun Jan 30, 2005 8:06 pm Post subject: Re: Elementary question on const |
|
|
On 30 Jan 2005 11:31:14 -0800 in comp.lang.c++, "Last Timer"
<dakshing64 (AT) yahoo (DOT) com> wrote,
| Quote: | please clarify what "const char* const data;" means? Thanks
|
The first "const" means the characters pointed to cannot be changed
via this pointer. The second one means the pointer value cannot be
changed.
|
|
| Back to top |
|
 |
Thomas Matthews Guest
|
|
| Back to top |
|
 |
Last Timer Guest
|
Posted: Sun Jan 30, 2005 8:30 pm Post subject: Re: Elementary question on const |
|
|
Thanks for your kind replies. Do you read it Left to Right or vice
versa to determine "first" and "second"?
The C++ for Dummies saz on pg 63
const char * pcc="This is a constant string";
char * const cpc="tjhis is also a string";
*pcc='a'; //illegal
*cpc ='b';//legal
pcc="another string" ; //legal
cpc="another string"; illegal
I'm trying to develop a mnemonic to remember these. Instead of rote
learning may be someone can help me figure this out logically.
Thanks again.
|
|
| Back to top |
|
 |
Mike Wahler Guest
|
Posted: Sun Jan 30, 2005 8:54 pm Post subject: Re: Elementary question on const |
|
|
"Last Timer" <dakshing64 (AT) yahoo (DOT) com> wrote
| Quote: | Thanks for your kind replies. Do you read it Left to Right or vice
versa to determine "first" and "second"?
The C++ for Dummies saz on pg 63
const char * pcc="This is a constant string";
char * const cpc="tjhis is also a string";
*pcc='a'; //illegal
*cpc ='b';//legal
pcc="another string" ; //legal
cpc="another string"; illegal
I'm trying to develop a mnemonic to remember these. Instead of rote
learning may be someone can help me figure this out logically.
|
http://www.ericgiguere.com/articles/reading-c-declarations.html
Yes, it's about C, but applies to C++ as well. (But of course it
won't cover the 'C++-only' stuff such as class members, etc.
I don't know of a web link for that, but this one should give you
a good start.)
Also, google for a utility called 'cdecl', which can take
a C declaration and translate it to English. It's available
for most platforms, and is supplied with some.
-Mike
|
|
| Back to top |
|
 |
|