473,385 Members | 1,912 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,385 software developers and data experts.

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();
} ///:~

Jul 23 '05 #1
5 1393

"Last Timer" <da********@yahoo.com> wrote in message
news:11*********************@c13g2000cwb.googlegro 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
Jul 23 '05 #2
On 30 Jan 2005 11:31:14 -0800 in comp.lang.c++, "Last Timer"
<da********@yahoo.com> 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.

Jul 23 '05 #3
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

Jul 23 '05 #4
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.

Jul 23 '05 #5
"Last Timer" <da********@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr 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
Jul 23 '05 #6

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

Similar topics

15
by: Dave | last post by:
Hello NG, It is well known that memory-allocating definitions should not be put in a header file. I believe, however, that this does not apply to const definitions. For example: #ifndef...
5
by: Lionel B | last post by:
Greetings, I am trying to implement "element-wise" arithmetic operators for a class along the following lines (this is a simplified example): // ----- BEGIN CODE ----- struct X { int a,b;
29
by: Merrill & Michele | last post by:
I'm now looking at page 115 K&R. After having discussed counterexamples, I shall herewith and henceforth make all my c programs look like int main(int orange, char* apple) {return(0);} Q1) ...
20
by: Merrill & Michele | last post by:
I am determining how best to call an already compiled c program from another. The following is a program that compiles and shows the information passed: //Text1.cpp #include <stdio.h> int...
0
by: Geoffrey L. Collier | last post by:
I wrote a bunch of code to do elementary statistics in VB, and would prefer to find code in C# to recoding all of the VB code. A search of the web found very little. Does anyone know of a good...
5
by: bromio | last post by:
can someone help me to make code for digital clock using AT89S52. The specs of clock are 12 hour clock,am-pm display,use timer interrupts to have delay.two external inputs to adjust hours and...
0
by: d3x0xr | last post by:
Heh, spelled out in black and white even :) Const is useles... do NOT follow the path of considering any data consatant, because in time, you will have references to it that C does not handle,...
11
by: pauldepstein | last post by:
I am using the xlw package as a c++ wrapper for excel functions. In our c++ library, there is an excel function which our c++ developers refer to as DevelopersFunction, and which the excel users...
4
by: tonywh00t | last post by:
Hi everyone, I have a "simple" question, especially for people familiar with regex. I need to parse strings that have the form: 1:3::5:9 which indicates the set of integers {1 3 4 5 9}. In...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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,...
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.