const-correctness

Is there some ninja trick to make a variable constant after its declaration?

I know the answer is 99.99% no, but I figured it was worth a try, you never know. void SomeFunction(int a) { // Here some processing happens on a, for example: a *= 50; a %= 10; if(example()) a = 0; // From this point on I want to make "a" const; I don't want to allow // any code past this comment to modif...

Adding const-ness after the fact in C++

Possible Duplicate: Is there some ninja trick to make a variable constant after its declaration? Consider the following minimal example: void MutateData(std::string&); int main() { std::string data = "something that makes sense to humans."; ::MutateData(data); // Mutates 'data' -- e.g., only changes the order of the cha...

Avoiding code duplication (const-correctness) redux

I was reading this question here here regarding const-correctness. The Scott Meyer solution seems like a good work-around, but what if you have a member function (which requires both a const and non-const version) that makes use of the this pointer. If the member function is const then this automatically means const this, which can mak...

can the functors called from algorithms acting on a map accept pair<K, V> instead of value_type?

I tried to write a short function to invert an std::map<K, V> (I know about boost.bimap, this is for self-education), and found, to my surprise, that the code that GCC 4.4 accepted with -pedantic -ansi settings was rejected as const-incorrect by SunCC (5.8, from 2005). Since value_type is std::pair<const K, V>, SunCC insisted that I con...

Why is the endptr parameter to strtof and strtod a pointer to a non-const char pointer?

The standard C library functions strtof and strtod have the following signatures: float strtof(const char *str, char **endptr); double strtod(const char *str, char **endptr); They each decompose the input string, str, into three parts: An initial, possibly-empty, sequence of whitespace A "subject sequence" of characters that repres...

Qt4 C++ Pointer to const QList of pointers.

I got stuck with pointer to const QList of pointers to Foo. I pass pointer to myListOfFoo from Bar object to Qux. I use pointer to const to prevent making any changes outside Bar class. The problem is that I'm still able to modify ID_ executing setID in Qux::test(). #include <QtCore/QCoreApplication> #include <QList> #include <iostream>...

What's the correct way to use const in C++?

const correctness has me somewhat confused. What rule of thumb do you use to decide when something should be const or not? e.g. consider this example class MyClass { string ToString(); // this one? const string& ToString(); // or this? const string& ToString() const; // or this? char* ToString(); // What about this? const ...

const and no const methods in c++ ?

Hi, I have a program and many of its classes have some operators and methods with the keyword const like the followings: operator const char* () const; operator char* (); void Save(const char *name) const; void Load(const char *name); First: what does it mean const at the end of the method declaration?, is it the same like putting it...