Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Elementary question on const

Reply
Thread Tools

Elementary question on const

 
 
Last Timer
Guest
Posts: n/a
 
      01-30-2005
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();
} ///:~

 
Reply With Quote
 
 
 
 
Mike Wahler
Guest
Posts: n/a
 
      01-30-2005

"Last Timer" <> wrote in message
news: ups.com...
> 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?

> //: 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


 
Reply With Quote
 
 
 
 
David Harmon
Guest
Posts: n/a
 
      01-30-2005
On 30 Jan 2005 11:31:14 -0800 in comp.lang.c++, "Last Timer"
<> wrote,
>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.

 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      01-30-2005
Last Timer wrote:
> I encountered the following code in Bruce Eckel's online book. Can you
> please clarify what "const char* const data;" means? Thanks


Pointers have two attributes that can be constant:
the content of the pointer and the object {date} it
points to.

Many people say that reading from right to left
is easier.
const char * const data;
^ ^-- The pointer is constant.
|-- The data is constant.

When the pointer is constant, it cannot point
to other objects. When the data is constant,
the data cannot be modified by dereferencing
the pointer.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library

 
Reply With Quote
 
Last Timer
Guest
Posts: n/a
 
      01-30-2005
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.

 
Reply With Quote
 
Mike Wahler
Guest
Posts: n/a
 
      01-30-2005
"Last Timer" <> wrote in message
news: oups.com...
> 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/...larations.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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const vector<const MyType> Vs const vector<MyType> magnus.moraberg@gmail.com C++ 2 02-09-2009 10:45 PM
is const necessary in eg int compar(const void *, const void *) lovecreatesbeauty@gmail.c0m C Programming 26 11-10-2008 09:47 PM
const correctness - should C++ prefer const member over non-const? fungus C++ 13 10-31-2008 05:33 AM
const vector<A> vs vector<const A> vs const vector<const A> Javier C++ 2 09-04-2007 08:46 PM
Casting int'** to 'const int * const * const' dosn't work, why? Jonas.Holmsten@gmail.com C Programming 11 07-01-2007 06:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57