const

What's the more elegant way to declare a const in C#

I'm refactoring a library in C# and I found a lot of upper case constants: INTERVAL, TIME, SECONDS. I think this a kind of unnecessary, and personally I prefer declare everything with camel case. Exist some exactly definition about the better way? ...

Why can Array.Length and Array.Count() not be assigned to const values?

I wanted to follow some excellent C++ advice of calculating the array length once and then using a value without having to call a function like so: Instead of: for( int i = 0; i < arr.length; ++i ) I write const int size = arr.length; // or arr.Count() for( int i = 0; i < size; ++i ) After reading a different thread (Is it costly ...

Argument of type "volatile char *" is incompatible with parameter of type "const char *"

I have a function whose prototype is as follows: void foo(const char * data); Elsewhere in my code, I have a global variable declared as follows volatile char var[100]; Whenever I try to do this: foo(var); The compiler throws up the following error message: Argument of type "volatile char *" is incompatible with parameter of ty...

Initializing C++ const fields after the constructor

I want to create an immutable data structure which, say, can be initialized from a file. class Image { public: const int width,height; Image(const char *filename) { MetaData md((readDataFromFile(filename))); width = md.width(); // Error! width is const height = md.height(); // Error! height is const } }; Wha...

Changing a classes member through an iterator

I'm learning C++ and can't get my head around this problem: I have a simple class A class A { private: int ival; float fval; public: A(int i = 0, float f = 0.0) : ival(i), fval(f) { } ~A(){ } void show() const { cout << ival << " : " << fval << "\n"; } void setVal(int i) { ival = i; } ...

const type qualifier soon after the function name

Might be a basic question.Still i am eager to know it from this forum as i like the explanations given here. In C++ some times i see members like below: return_type Function_name( datatype parameter1, datatype parameter2 ) const { /*................*/} what does this const type qualifier exact do over here. ...

Const method that modifies *this without const_cast

The following pattern has arisen in a program I'm writing. I hope it's not too contrived, but it manages to mutate a Foo object in the const method Foo::Questionable() const, without use of any const_cast or similar. Basically, Foo stores a reference to FooOwner and vice versa, and in Questionable(), Foo manages to modify itself in a c...

C: declare a constant pointer to an array of constant characters

Hi, I am trying to understand array declarations, constness, and their resulting variable types. The following is allowed (by my compiler): char s01[] = "abc" ; // typeof(s01) = char* const char s02[] = "abc" ; // typeof(s02) = const char* (== char const*) char const s03[] = "abc" ; // typeof(s03) = char const* (== const char...

Const confusion in C++

Possible Duplicate: Why is my return type meaningless? Hi, I'm confused about a particular const conversion. I have something like // Returns a pointer that cannot be modified, // although the value it points to can be modified. double* const foo() { static double bar = 3.14; return &bar; } int main() ...

Doxygen link to const function

Hi, I've got the following function prototype: bool key_pressed(enum key key) const; I documented it using doxygen. Now I have an overloaded version of that function that does the same, so I wanted to copy the doxygen comment like this: /// @copydoc key_pressed(enum key) const bool key_pressed(char key) const; This does not work, ...

Const return types in C

I was reading some samples of code, and they returned a const int. When I tried to compile the examples code I got errors concerning conflicting return types. So I started searching, thinking that the const was the problem (when I removed it, the code worked fine, not only did it compile, but worked as expected). But I never was able...

Accessibility of variables.

HI, i have small doubt related to the accessibility of variables. int i; //default the linkage is external const int i; //default linkage is internal extern int i; //explicitly assigning linkage as external class a { int l; //Default linkage is external void f() { int k; //default linkage is e...

Const pointer in a class object-oriented bug

Hello I have a simple example below that does not compile. i get the following warrning about const Error message: error C2662: 'Cfoo::GetNum' : cannot convert 'this' pointer from 'const Cfoo' to 'Cfoo &' Conversion loses qualifiers class Cfoo { public: bool RunMe( const Cfoo * bar ) { int i = bar->GetNum()...

Should I be making things const?

Possible Duplicate: Sell me on using const correctness In C (and I guess C++ as well), is there any particular reason to declare a variable const other than to prevent yourself from modifying it by mistake later on? Are const variables treated differently at compile time other than the fact that they aren't allowed to be modif...

cast const Class using dynamic_cast

I want to cast this: class Base { public: virtual ~Base(){}; }; class Der : public Base {}; int main() { const Base* base = new Der; Der* der = dynamic_cast<Der*>(base); // Error return 0; } What should I do? I tried to put: const Der* der = dynamic_cast<Der*>(base); to mantain the const but this doesn't work. ...

Is there any benefit to decalre a constant in a local scope in C#?

Hi, Is there any benefit to declare a local variable as "const" if I know that I won't be chaning its value? Thanks, ...

Reference Member Required to be Const?

In this simple example, why do I need to make 'member' const in order to get this to compile? struct ClassA { ClassA(int integer) {} }; struct ClassB { ClassB(int integer): member(integer) { } const ClassA& member; }; int main() { ClassB* b = new ClassB(12); return 0; } Otherwise, I get this err...

Is it possible to "constify" a field of `std::pair` without hacks?

In C++, the compiling the following code: std::pair <int, int> x; static_cast <std::pair <const int, int>*> (&x); gives an error: error: invalid static_cast from type ‘std::pair<int, int>*’ to type ‘std::pair<const int, int>*’ I more or less understand why it happens, as cv-qualifying a type in a template parameter list can, in pr...

How to use constants with Complex (curly) syntax?

I was surprised to see that the following doesn't work as expected. define('CONST_TEST','Some string'); echo "What is the value of {CONST_TEST} going to be?"; outputs: What is the value of {CONST_TEST} going to be? Is there a way to resolve constants within curly braces? Yes, I am aware I could just do echo "What is the value of "....

What is the difference between const and static vars?

If I have a class and want to have some static vars, which would be the correct way to declare them? For example class Foo{     public static $var1     public static $var2     public static $var3     ...... } OR class Foo{     const $var1     const $var2     const $var3     ...... } both ways let me use Foo::var1...