constants
const before parameter vs const after function name in C++
The first form means that the (state of the) Circle object bound to the reference which is the parameter of the copy() function will not be altered by copy() through that reference. The reference is a reference to const, so it won’t be possible to invoke member functions of Circle through that reference which are … Read more
What is the difference in const-correctness between C and C++?
In addition to the differences you cite, and the library differences that Steve Jessop mentions, char* p1; char const* const* p2 = &p1; is legal in C++, but not in C. Historically, this is because C originally allowed: char* p1; char const** p2 = &p1; Shortly before the standard was adopted, someone realized that this … Read more
Why can’t I access elements with operator[] in a const std::map?
at() is a new method for std::map in C++11. Rather than insert a new default constructed element as operator[] does if an element with the given key does not exist, it throws a std::out_of_range exception. (This is similar to the behaviour of at() for deque and vector.) Because of this behaviour it makes sense for … Read more
Why is std::numeric_limits::max() a function?
To expand on Neil’s remark, std::numeric_limit<T> is available for any number type including floating point numbers, and if you dig through the comp.lang.c++ thread, you’ll see the mention that it might not be possible to define the static variables for floating point values. So, for consistency they decided to put both integral and floating points … Read more
Initialize a constant sized array in an initializer list
While not available in C++03, C++11 introduces extended initializer lists. You can indeed do it if using a compiler compliant with the C++11 standard. struct Test { Test() : set { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } { }; int set[10]; }; The above code compiles fine using g++ -std=c++0x … Read more
Modifying reference member from const member function in C++
Because you are not changing any variable in X. Actually, you are changing _y which is an outsider with respect to your class. Don’t forget that: y = newY; Is assigning the value of newY to the variable pointed by y, but not the references them selves. Only on initialization the references are considered.
C# “Constant Objects” to use as default parameters
No, default values for optional parameters are required to be compile-time constants. In your case, a workaround would be: public void doSomething(SettingsClass settings = null) { settings = settings ?? DefaultSettings; … }
How to call a non-const function within a const function (C++)
int Random() const { return var_ ? const_cast<ClassType*>(this)->newCall(4) : 0; } But it’s not a good idea. Avoid if it’s possible!