Factbites
 Where results make sense
About us   |   Why use us?   |   Reviews   |   PR   |   Contact us  

Topic: Const correctness


Related Topics

In the News (Fri 1 Jun 12)

  
  Const Correctness - C++ Tutorials - Cprogramming.com
For instance, const references allow you to specify that the data referred to won't be changed; this means that you can use const references as a simple and immediate way of improving performance for any function that currently takes objects by value without having to worry that your function might modify the data.
Once you have a const object, it cannot be assigned to a non-const reference or use functions that are known to be capable of changing the state of the object.
Although returning a const reference prevents anyone from changing the data by using it, it means that you have to have persistent data to back the reference--it has to actually be a field of the object and not temporary data created in the function.
cprogramming.com /tutorial/const_correctness.html   (2274 words)

  
 /* Rambling comments... */: Const correctness
Using const correctly and consistently means that when you look at a piece of code you can be certain that some types are not modified by the code that you're looking at or by code it calls.
Being const correct means you need to think a little bit more when you write the code but you can trade that for thinking a little bit less when you read the code.
Correct use of const tends to drive a design in the right direction; if some of the code needs to modify an object and some of the code doesn't then const will help you factor the code appropriately by building walls between areas of code that operate in different ways.
www.lenholgate.com /archives/000283.html   (1409 words)

  
 Const correctness - Wikipedia, the free encyclopedia
In computer science, const-correctness is the form of program correctness that deals with the proper declaration of objects as mutable or immutable.
int i = 42; int const and refToConst = i; // OK int and const constRef = i; // Error the "const" is redundant
The enhancement request ticket in the Java community process for implementing const correctness in Java was recently closed, implying that const correctness will probably never find its way into the official Java specification.
en.wikipedia.org /wiki/Const_correctness   (1739 words)

  
 [No title]   (Site not responding. Last check: 2007-10-20)
The three functions below show the const- correct forms corresponding to the examples above: void g1(const int i); void g2(const int &i); void g3(const int *i); g1 has the same effect as f1 as far as the user is concerned.
const int* const i; This declares a constant pointer to a constant integer.
Const Correctness Part III by Darren Collins -------------------- In last week's article, I said that the declaration const int *i; is more common than int const *i; even though they mean the same thing.
www.eng.auburn.edu /users/palliki/cplus/const.txt   (3083 words)

  
 Bug ID: 4211070 Java should support const parameters (like C++) for code maintainence
I'm a fan of const correctness too, but then you need a mechanism like the andquot;mutableandquot; keyword in C++ to notify the compiler of member data that may be modified without violating the spirit of const.
Const on a class would assert that the pbjects were immutable -- same as const on its non-constructor methods and non-private ivars.
It is perfectly permissible to have a const reference and a non- const reference to the same object, and to have the non- const reference be used to modify the object, thus changing the state observed by the const reference.
bugs.sun.com /bugdatabase/view_bug.do?bug_id=4211070   (12939 words)

  
 C++ const Correctness   (Site not responding. Last check: 2007-10-20)
When designing const member functions, you may encounter a situation in which altering the internal state of the class is necessary.
Using const member functions increases encapsulation by restricting the ways in which an object can be used in certain circumstances, particularly when objects are passed by constant reference to functions or member functions.
A class designed without const member functions hamstrings those who would use the class, as there otherwise would be no way to use the class when objects of its type are passed by const references.
www.linuxjournal.com /node/7629/print   (1883 words)

  
 C++ const Correctness | Linux Journal   (Site not responding. Last check: 2007-10-20)
A pointer is a variable itself, so the placement of const often determines whether it applies directly to the pointer, to the object pointed to or to both.
A proper const interface allows the compiler to do a lot of type-checking work for you in situations where constant objects are used.
Using const temps also forces you to use proper scoping and init-on-declaration, which is a worthwhile goal in itself (it saves ctor and assignment operator calls).
www.linuxjournal.com /article.php?sid=7629   (3173 words)

  
 The Old Joel on Software Forum - const--use it?
Const correctness is part of good OO design, and understanding how the design maps to C++.
However, if someone else doesn't mark their method as taking a const parameter, or being a const method, you'll have to remove the constness from your object to allow it to be used with non-const correct code.
Const is well worth learning to use properly, it's easy to know if you need it at the time you're writing the code and most people who use C++ shouldn't be doing so...
discuss.fogcreek.com /joelonsoftware?cmd=show&ixPost=124114   (1943 words)

  
 Const-Correctness
Even though it modifies the object's internal state, this function should be const because the object's observable state is unchanged (we are doing some caching, but that's an implementation detail and the object is logically const).
This 'const' is useless, since references cannot be changed to refer to a different object anyway.
This 'const' is equally useless, but for a different reason: since you're passing the pointer by value, this makes as little sense as passing a parameter 'const int' above.
www.cs.bgu.ac.il /~spl051/Personal_material/Practical_sessions/Ps_8/ps8.html   (805 words)

  
 GotW #81: Constant Optimization?
Other than that, however, writing const in Example 3 is not an optimization for most classes Z -- and in those cases where it is an optimization, it's not a compiler code generation optimization.
Const is indeed a Good Thing, but the point of this issue of GotW is that const is mainly for humans, rather than for compilers and optimizers.
Even when it comes to optimization, const is still principally useful as a tool that lets human class designers better implement handcrafted optimizations, and less so as a tag for omniscient compilers to automatically generate better code.
www.gotw.ca /gotw/081.htm   (1259 words)

  
 C++ FAQ (part 6 of 8)
It is almost as if a const String, for example, is a different class than an ordinary String, since the const variant is missing the various mutative operations in the non-const variant (e.g., you can imagine that a const String simply doesn't have an assignment operator).
The problem with using "Fred constand x" (with the const before the and) is that it could easily be mis-typed as the nonsensical "Fred andconst x"[18.7] (with the const after the and).
Because "const int* p" means "p promises not to change the *p," not "*p promises not to change." Causing a const int* to point to an int doesn't const-ify the int.
www.angelfire.com /ma/mrao/cpp6.html   (6028 words)

  
 The Old New Thing : Why doesn't C# have "const"?
He's for adding const ness, saying that the design decision of not adding it is short sighted, like the earlier C++ design decisions (such as putting this at the end of a class).
A pragmatic strategy consisting of enabling const in the system and the languages, while leaving the libraries as they are, would allow Morts to keep dodging malpractice suits with their broken code while simultaneously allowing professionals to practice with the care they want to take.
I don't have a boatload of const casting in C++; the only time I const_cast is when interoperating with legacy C void*-using APIs; as I normally const my locals (just because they're called variables doesn't mean they're meant to vary) occasionally a const_cast is required.
blogs.msdn.com /oldnewthing/archive/2004/04/27/121049.aspx   (4125 words)

  
 ⊙ AntiQuark: Const correctness in C#   (Site not responding. Last check: 2007-10-20)
Const is a bullet in the arsenal of preemptive bug prevention.
Const isn't an cure-all by any means, but it helps eliminate certain typo (or worse, "thinko") bugs in which you alter a variable, even though your intention was to leave it untouched.
When asked why C# didn't have const, the lead architect sort of looked at his shoes and mumbled that it was too hard to guarantee constness, and that C++ cheats by letting you override it.
www.antiquark.com /2005/08/const-correctness-in-c.html   (550 words)

  
 November 1996/C++ Theory and Practice   (Site not responding. Last check: 2007-10-20)
A "physically const" object is one that truly immutable or unchangeable, and therefore may reside in a read-only data region.
During the call strlen(title), *s is a logically const expression that refers to a physically const object.
My recollection is that she claimed that she used an architecture in which the compiler could take advantage of the const parameter to generate better (faster or tighter) code than it could if the parameter were non-const.
www.tcnj.edu /~hernande/cujv5/html/14.11/saks/saks.htm   (2425 words)

  
 The Old Joel on Software Forum - The importance of being const
I'm sure that all readers of JoS are careful to be "const correct" at all times, but it’s kind of depressing to see this sort of misleading garbage from a smart-alec know-nothing of the younger generation.
I would like to be const correct, but there is no chance of it with the code I work with.
Also, you can use const correctness with Managed C++ as usual, but the const correctness does not propagate to any non-C++ modules (including the Framework library) that are linked with your program.
discuss.fogcreek.com /joelonsoftware?cmd=show&ixPost=161596   (1606 words)

  
 [No title]
Fred *const p; p is a const pointer to a Fred.
So you cannot change the pointer p or change Fred via p const Fred &x; X aliases a Fred object but x cannot be used to change the Fred object.
Can also be written: Fred const& x (little used) void inspect() const; member promises not to change *this void Mutate(); member might change *this A const member function is one that inspects rather than mutates its object.
www.toymaker.info /Games/Const_Correctness.doc   (211 words)

  
 Cprogramming.com: Site Update History
A pointer to const data does not allow modification of the data through the pointer.
The declaration of const data merely requires that the const precede the *, so either of the following two declarations are valid.
Because the address is const, the pointer must be assigned a value immediately.
www.cprogramming.com /reference/pointers/const_pointers.html   (168 words)

  
 CodeCraft - Const Correctness Part 1   (Site not responding. Last check: 2007-10-20)
You can tell the caller (and the compiler), through the function prototype, that a given parameter to a function won't be modified within that function.
Correctness: If you accidentally modify a variable that you shouldn't, you'll catch the error at compile time rather than waiting for it to bite you at run time.
Likewise, the revPrintVec function should be modified to pass (const vector andv) and use a const_reverse_iterator.
codecraft.pool-room.com /Cpp/const-correctness-1.html   (950 words)

  
 Const Correctness
The problem with const in interfaces is that it forces you to make a long ranging decision, that may or may not be valid, and will be painful to change.
So const is a special case of the idea that you sometimes want to restrict certain clients to a subset of an interface, and const correctness is just a kind of static type safety.
If the intent of the method changes away from what was prescribed by the const declarations, I think it is very questionable whether you are using the same function or overlaying a new operation on an existing name (the latter is bad).
c2.com /cgi/wiki?ConstCorrectness   (3259 words)

  
 Const-Correctness in C++
A pointer to a const object can be initialized with a pointer to an object that is not const, but not vice versa.
An ordinary string literal has type "array of n const char" and static storage duration (_basic.stc_), where n is the size of the string as defined below, and is initialized with the given characters.
A wide string literal has type "array of n const wchar_t" and has static storage duration, where n is the size of the string as defined below, and is initialized with the given charac- ters.
www.possibility.com /Cpp/const.html   (3253 words)

  
 const | | Dictionary & Translation by Babylon
In computer science, const-correctness is the form of program correctness that deals with the proper declaration of objects as mutable or immutable.
The term is mostly used in a C or C++ context, and takes its name from the const keyword in those languages.The idea of const-ness does not imply that the variable as it is stored in the computer's memory is unwriteable.
Rather, const-ness is a compile-time construct that indicates what a programmer may do, not necessarily what he or she can do.In addition, a class method can be declared as const, indicating that calling that method does not change the object.
www.babylon.com /definition/const/All   (268 words)

  
 Const correctness: Facts and details from Encyclopedia Topic   (Site not responding. Last check: 2007-10-20)
Const correctness: Facts and details from Encyclopedia Topic
int const & refToConst = i; // Ok int & const constRef = i; // const is redundant; may not compile
const int * volatile const tableLookup = reinterpret_cast
www.absoluteastronomy.com /encyclopedia/c/co/const_correctness.htm   (1164 words)

  
 Thoughts on const usage in C++? - GameDev.Net Discussion Forums   (Site not responding. Last check: 2007-10-20)
If there was an object passed, I still wouldn't use a const reference, or even a const pointer, I'd simply pass in a pointer to the object, and that's it.
Let's face it, there are uses for const, ie; wanting to return a member of a certain class, and surely you don't want to pass it as a non-const to prevent the client from changing data.
Passing const references to builtin types is a little asinine, but const references are very good for passing your own objects.
www.gamedev.net /community/forums/topic.asp?topic_id=296283   (1546 words)

  
 GotW #6: Const-Correctness
Even though it modifies the object's internal state, this function should be const because the object's observable state is unchanged (we are doing some caching, but that's an implementation detail and the object is logically const).
This 'const' is useless, since references cannot be changed to refer to a different object anyway.
This 'const' is equally useless, but for a different reason: since you're passing the pointer by value, this makes as little sense as passing a parameter 'const int' above.
www.gotw.ca /gotw/006.htm   (823 words)

  
 Allegro `const'-correctness
Callback functions which do not treat relevant parameters as `const' are, in a small (but potentially signficant) way, broken.
This is because it is valid to pass either a `char**' or a `const char**', but unfortunately there is no way to tell the compiler exactly what we mean.
Therefore, a const BITMAP parameter does not make sense, and is not used throughout the library.
www.nsac.ns.ca /eng/courses/temp/allegro/const.html   (656 words)

  
 Technical Interview Questions » const Correctness: Difference between Foo* const ptr, const Foo* ptr, const Foo* ...   (Site not responding. Last check: 2007-10-20)
Foo* const ptr: ptr is a const pointer to a Foo Object.
const Foo* ptr: ptr points to a Foo object that is const.
const Foo* const ptr: ptr is a const pointer to a const Foo object.
www.tekpool.com /?p=53   (159 words)

  
 C++ FAQ (part 6 of 8)   (Site not responding. Last check: 2007-10-20)
For example, if class Fred has a const member function[18.9] called inspect(), saying p-> inspect() is OK. But if class Fred has a non-const member function[18.9] called mutate(), saying p-> mutate() is an error (the error is caught by the compiler; no run-time tests are done, which means const doesn't slow your program down).
The problem with using "Fred const& x" (with the const before the &) is that it could easily be mis-typed as the nonsensical "Fred & const x"[18.7] (with the const after the &).
E.g., in Set::lookup() const, you might say, Set* self = const_cast< Set*> (this); After this line, self will have the same bits as this (e.g., self == this), but self is a Set* rather than a const Set*.
omicron.felk.cvut.cz /FAQ/articles/a2742.html   (6054 words)

  
 CppConst at CodePedia
A consequent use of the const keyword is termed const correctness.
A function declaration is the first impression of a function as it summarizes the effect of the variables involved or produced.
When using pointers, const can be used twice for every argument.
www.codepedia.com /1/CppConst   (829 words)

Try your search on: Qwika (all wikis)

Factbites
  About us   |   Why use us?   |   Reviews   |   Press   |   Contact us  
Copyright © 2005-2007 www.factbites.com Usage implies agreement with terms.