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

help! confusing "const"?

Hi all,

I just couldn't get myself clear about the usage of "const" in front
of and/or behind variables, pointers, classes, objects and
functions...

It's too confusing... any good clear article/tutorial that can help
me?

Thanks a lot!
Jul 6 '08 #1
7 1907
Luna Moon wrote:
I just couldn't get myself clear about the usage of "const" in front
of and/or behind variables, pointers, classes, objects and
functions...

It's too confusing... any good clear article/tutorial that can help
me?
http://www.parashift.com/c++-faq-lit...rrectness.html
--
Christian Hackl
Jul 6 '08 #2

"Luna Moon" <lu**********@gmail.comwrote in message
news:22**********************************@x41g2000 hsb.googlegroups.com...
Hi all,

I just couldn't get myself clear about the usage of "const" in front
of and/or behind variables, pointers, classes, objects and
What you're calling 'variables, pointers, and objects' are
all known as 'objects' in C++. Any object can be made const
or not. THere's no such thing as a const function (however
there can be a const pointer to a function.

A 'class' is just another name for 'type'. Keep the above
in mind when reading the link I cite below.
functions...

It's too confusing... any good clear article/tutorial that can help
me?

Thanks a lot!
http://www.timfanelli.com/item/26

-Mike
Jul 6 '08 #3

"Mike Wahler" <mk******@mkwahler.netwrote in message
news:kN******************************@earthlink.co m...
all known as 'objects' in C++. Any object can be made const
or not. THere's no such thing as a const function (however
there can be a const pointer to a function.
I misspoke. There is indeed a construct known as a 'const
member function'. Its declaration takes the form:

T func() const;

This indicates that the function will not modify the
state of the object for which it was invoked. An
attempt to do so should cause a compiler diagnostic.
-Mike
Jul 6 '08 #4
"Luna Moon" <lu**********@gmail.comwrote in message
news:22**********************************@x41g2000 hsb.googlegroups.com...
Hi all,

I just couldn't get myself clear about the usage of "const" in front
of and/or behind variables, pointers, classes, objects and
functions...

It's too confusing... any good clear article/tutorial that can help
me?

Thanks a lot!
const just means "this isn't going to change." and what it's next to states
what isn't going to change.

const <typeVarname; Contents of VarName isn't going to change.
<typeconst Varname; Same as above.
<typeconst * Varname; Contents of Varname won't change.
<type* const Varname; Where Varname points to won't change
<typefunction() const; Used for classes, function will not change
anything for the class.
<typefunction<const <typeVarname); Contents of local variable Vaname
will not change.

The easiest way is to read it from right to left. I.E.

const <typeVarname.
Read fom Varname to the left. "Varname is a <typethat is constant."
<typeconst Varname
"Varname is a constant <type>" Same as above can be done either way.
<typeconst * Varname; "Varname is a pointer to a constant <type>"
<type* const Varname; "Varname is a constant pointer to <type>"

The only one I don't do this for is for a const on a function. I just know
it's a constant function.
<type:Functionname( <typeparm ) const
That function doen't change anything about the class (not allowed to change
class variables)

Jul 6 '08 #5

"Luna Moon" <lu**********@gmail.coma écrit dans le message de news:
22**********************************...oglegroups.com...
Hi all,

I just couldn't get myself clear about the usage of "const" in front
of and/or behind variables, pointers, classes, objects and
functions...

It's too confusing... any good clear article/tutorial that can help
me?

Thanks a lot!
int main()
{
int notConst = 1;

const int a1 = 0; // a constant integer
int const a2 = 0; // same as a1
a1++; // error a1 is const
a2++; // error a2 is const

const int& a3 = notConst;
int const& a4 = notConst;
notConst++; // Ok it is not const
a3++; // error a3 is const even if it is a reference to a non const
variable
a4 = a1; // error, a4 is just like a3

const int *a5 = &notConst; // pointer to a const value (notConst can be
changed but not via a5)
int const* a6 = &a1; // same as a5
// now it is getting interesting
*a5 = 45; // error trying to change the value a5 is pointing to
a5 = NULL; // ok the value a5 was pointing to is not changed.
a6 = &notConst; // ok a1 has not changed
*a6 = 7; // error notConst cannot be changed via a6

// same error here as with a3 and a4
const& int a7 = a3;
a7++; // error
a7 = a3; // error

// a8 is a const pointer to a non const int
int* const a8 = &a1; // error must point to a non const value
int* const a9 = &notConst; // ok
*a9 = 8; // ok, the pointer has not changed, only the value it point to
a9 = NULL; // error, trying to change the pointer

// const pointer to a const value
const int* const a10 = &notConst;
*a10 = 9; // error
int const* const a11 = &notConst; // same as a10
int const* const a12 = &a1;
a12 = NULL; // error
*a2 = 99; // error

return 0;
}
Jul 6 '08 #6
In article <xI***************@newsfe05.lga>, ta*******@rocketmail.com
says...

[ ... ]
const just means "this isn't going to change." and what it's next to states
what isn't going to change.
Not really. It really means something close to 'read-only'. It's
possible (for example) to define a pointer to something that's both
const AND volatile, meaning this code won't change it, but something
else might. Even the "read-only" requires a few qualifications of its
own -- a const member function can't write to normal members of the
object on which it's invoked, but it can write to mutable members.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 6 '08 #7
On Jul 6, 8:15 pm, Luna Moon <lunamoonm...@gmail.comwrote:
I just couldn't get myself clear about the usage of "const" in
front of and/or behind variables, pointers, classes, objects
and functions...
It's too confusing... any good clear article/tutorial that can
help me?
It's not really that complicated. The simplest rule when
writing code is that the const goes behind whatever it's
modifying. In a lot of cases, the language doesn't give you a
choice, so you might as well be consistent. (It also helps in
understanding the effects of const when typedef's are involved.)

Beyond that, const has two more or less distinct meanings. If
it applies to an object, it means that the object itself is
const, and any attempt to modify it is undefined behavior---the
compiler can put it in write only memory, or simply assume that
its value hasn't changed, for optimization purposes. (There is
one exception to this: mutable members of const class type
objects.)

The second involves the way const works in the type system: the
type "int const" is not the same type as "int", and there are
operations which are allowed on an expression of type "int", but
not on an expression of type "int const". As explained above,
if the expression denotes an actual object of the type const,
those operations result in undefined behavior, but of course, a
const type can appear in expressions even when no const object
is involved. Thus, the typename "int const*" refers to a
pointer to a const int, and "int *const" refers to a const
pointer to a (non-const) int: both declare an object of pointer
type, but in the case of the first, this object (the pointer) is
not const; the only role of the const here is in the type
system. The type system also has the concept of a const member
function; in this case, the const affects the type of the this
pointer, which becomes T const*, rather than T*.

Note too that when you define a class yourself, you can more or
less define what "const" means when applied to that class.
Thus, for example, std::vector defines const to mean that you
cannot modify the elements in the vector (or more correctly, the
elements in the vector are also "const", with whatever meaning
const has for them), even though they are separate "objects".

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jul 7 '08 #8

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

Similar topics

20
by: da Vinci | last post by:
Hello again. I have a question regaring pass-by-reference and multiple functions. This is an assignment that I have to use pass-by-reference for everything. First off, I made the following...
5
by: ali | last post by:
Hi, I'm trying to understand the reason for different output on the following codes Code1: #include <iostream.h> int main()
0
by: Christopher Ambler | last post by:
This is long, but it's driving me nuts. I need some adult supervision :-) (and I'm not above bribing for help) I have a stored procedure that I call that returns XML to me. The SP returns 3...
2
by: jwmckin | last post by:
I have 2 fields in 2 seperate tables with text strings as the data. The two fields do not have the same number of entries. I need to compare the larger of the 2 fields to the smaller field and look...
42
by: slickn_sly | last post by:
<code> line = 1; while( !feof( infile ) ) { if( line = 2 ) { fgets( name, 25, infile ); } else if( line = 3 ) {
5
by: serge calderara | last post by:
Dear all, I am new in asp.net and prepare myself for exam I still have dificulties to understand the difference between server control and HTML control. Okey things whcih are clear are the fact...
2
by: Steve R. Hastings | last post by:
While studying iterators and generator expressions, I started wishing I had some tools for processing the values. I wanted to be able to chain together a set of functions, sort of like the...
6
by: D | last post by:
Hello all...I have an issue with one of my java script functions that I'm hoping someone can easily help with. I have a web based application that we use to create/sign up for overtime. When we...
0
by: CoreyReynolds | last post by:
Hello, I've been looking at this for probably five hours now and I'm loosing my mind. I'll start by just showing my end result of this query of mine, since it illustrates the problem. Here...
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: 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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.