const

Referring to const value @ compile time - when is a const's definition really available?

I tried const int i[] = { 1, 2, 3, 4 }; float f[i[3]]; // g++ cries "error: array bound is not an integer constant" int main() { const int j[] = { 0, 1, 2, 3 }; float g[j[3]]; // compiler is happy :) return 0; } What is the difference between the two aggregates? How come referring to a const aggregate's e...

Is there way was to specify const unichar quoted strings?

Using xcode, I'm trying to get a const c string into a unichar array by doing the following: const unichar *str = "Test String"; but the "Test String" is typed to be (const char *) and produces compiler errors. Is there way to specify a "Test String" that is of type unichar? I was hoping for somethig like: const unichar *str = U"Te...

If changing a const object is undefined behavior then how do constructors and destructors operate with write access?

C++ standard says that modifying an object originally declared const is undefined behavior. But then how do constructors and destructors operate? class Class { public: Class() { Change(); } ~Class() { Change(); } void Change() { data = 0; } private: int data; }; //later: const Class object; //object.Change(); - won't co...

2 quick questions about passing by const pointer/const reference

1) Can someone explain the following? void OnCreate(HWND hWnd, const LPCREATESTRUCT lpCreateStruct) { lpCreateStruct->x = 2; // this compiles } void OnCreate(HWND hWnd, const CREATESTRUCT * lpCreateStruct) { lpCreateStruct->x = 2; // this does not compile } 2) Is it faster to pass by pointer or by reference? Or the same? ...

Shall I prefer constants over defines?

In C, shall I prefer constants over defines? I've reading a lot of code lately, and all of the examples make heavy use of defines. ...

Argument order for mixed const and non-const pass-by-reference

In keeping with the practice of using non-member functions where possible to improve encapsulation, I've written a number of classes that have declarations which look something like: void auxiliaryFunction( const Class& c, std::vector< double >& out); Its purpose is to do something with c's public member functions and ...

C#: Can parameters be constant?

I'm looking for the C# equivalent of Java's final. Does it exist? Does C# have anything like the following: public Foo(final int bar); In the above example, bar is a read only variable and cannot be changed by Foo(). Is there any way to do this in C#? For instance, maybe I have a long method that will be working with x, y, and z coo...

[newb] C++ const iterator C2662

Hi All, Having problems iterating. Problem has to do with const correctness, I think. I assume B::getGenerate() should be const for this code to work, but I don't have control over B::getGenerate(). Any help is greatly appreciated. Thanks in advance, jbu Code follows: int A::getNumOptions() const { int running_total = 0; BL...

Tinyxml to print attributes

I'm trying to get std::string from attribute's value with TinyXml. The only thing I can get is a const char * val, and I can't find any way to convert from const char * to a std::string. so two possible answers to that: 1. How to get a string of an attribute with TinyXml? 2. How to convert const char * val to string val. this is the co...

C++ static const and initialization (is there a fiasco)

Hi all, I am returning to C++ after a long absence and I am stumbling a little over my understanding of the fairly well known static initialization problem. Let's say I have a simple class Vector2 as given below (note that I am aware that x and y should be private with getters and setters, these have just been omitted for brevity): cl...

How to define type member constant in F# ?

In C# one can define a type member constant like this: class Foo { public const int Bar = 600; } The IL looks like this. .field public static literal int32 Bar = int32(600) How can I do the same within Visual F# / FSharp? I tried this to no avail: [<Sealed>] type Foo() = [<Literal>] let Bar = 600 ...

getting around const in an init method

So I can't use initializers in my class constructor because of using arrays, so I decided to use an init() method instead. Now I have a different problem. I have a class like this: class EPWM { private: volatile EPWM_REGS* const regs; public: void init(volatile EPWM_REGS* _regs); }; where I need to implement init() by initializi...

g++: const discards qualifiers

why do I get a discard qualifiers error: customExc.cpp: In member function ‘virtual const char* CustomException::what() const’: customExc.cpp: error: passing ‘const CustomException’ as ‘this’ argument of ‘char customException::code()’ discards qualifiers on the following code example #include <iostream> class CustomException: publi...

How often do you declare your functions to be const?

Do you find it helpful? ...

Modifying reference member from const member function in C++

I am working on const-correctness of my code and just wondered why this code compiles: class X { int x; int& y; public: X(int& _y):y(_y) { } void f(int& newY) const { //x = 3; would not work, that's fine y = newY; //does compile. Why? } }; int main(int argc, char **argv) { int i1=0, i2=0...

C++ language some live examples for mutable...

Respected sir , I need some help for mutable keyword it is used in a const function and please any body explain for the live example about the mutable and constant function and also diff. for the volatile member and function please help me in Advance Thank you, ...

How to provide stl like container with public const iterator and private non-const iterator?

Hello, I have a class that includes a std::list and wish to provide public begin() and end() for const_iterator and private begin() and end() for just plain iterator. However, the compiler is seeing the private version and complaining that it is private instead of using the public const version. I understand that C++ will not overload...

Const operator overloading problems in C++

I'm having trouble with overloading operator() with a const version: #include <iostream> #include <vector> using namespace std; class Matrix { public: Matrix(int m, int n) { vector<double> tmp(m, 0.0); data.resize(n, tmp); } ~Matrix() { } const double & operator()(int ii, int jj) const { cout ...

Does it ever make sense to make a fundamental (non-pointer) parameter const?

I recently had an exchange with another C++ developer about the following use of const: void Foo(const int bar); He felt that using const in this way was good practice. I argued that it does nothing for the caller of the function (since a copy of the argument was going to be passed, there is no additional guarantee of safety with reg...

C++: Why does gcc prefer non-const over const when accessing operator[]?

This question might be more appropriately asked regarding C++ in general, but as I am using gcc on linux that's the context. Consider the following program: #include <iostream> #include <map> #include <string> using namespace std; template <typename TKey, typename TValue> class Dictionary{ public: map<TKey, TValue> internal; ...