const

C++ const question.

If I do this: // In header class Foo { void foo(bar*); }; // In cpp void Foo::foo(bar* const pBar) { //Stuff } The compiler does not complain that the signatures for Foo::foo do not match. However if I had: void foo(const bar*); //In header void Foo::foo(bar*) {} //In cpp The code will fail to compile. What is going on? I'm usin...

Java: `enum` vs `String` as Parameters

I've been reading through the details of the System libraries set and get methods yet the parameters are usually Strings. Would you consider the use of String as parameters bad practise since the inclusion of enum? A better alternative at minimum might be public final String, No? ...

How do you declare arrays in a c++ header?

This is related to some other questions, such as: this, and some of my other questions. In this question, and others, we see we can declare and initialise string arrays in one nice step, for example: const char* const list[] = {"zip", "zam", "bam"}; //from other question This can be done in the implementation of a function with no bo...

Why does this const member function allow a member variable to be modified?

class String { private: char* rep; public: String (const char*); void toUpper() const; }; String :: String (const char* s) { rep = new char [strlen(s)+1]; strcpy (rep, s); } void String :: toUpper () const { for (int i = 0; rep [i]; i++) rep[i] = toupper(rep[i]); } int main () { ...

const and nonconst STL Iterators

What is the difference between a ::const_iterator and an ::iterator and where would you use one over the other? ...

In C++, I want my interface, .h to say int GetSomeInt() const;.... but actually the method *DOES* update "this". Is there a way to do it without changing the .h?

I'm adding some lazy initialization logic to a const method, which makes the method in fact not const. Is there a way for me to do this without having to remove the "const" from the public interface? int MyClass::GetSomeInt() const { // lazy logic if (m_bFirstTime) { m_bFirstTime = false; Do something once ...

What does a const pointer-to-pointer mean in C and in C++?

I know the rule-of-thumb to read declarations right-to-left and I was fairly sure I knew what was going on until a colleague told me that: const MyStructure** ppMyStruct; means "ppMyStruct is a pointer to a const pointer to a (mutable) MyStructure" (in C++). I would have thought it meant "ppMyStruct is a pointer to a pointer to a con...

How does one declare an array of constant function pointers in C?

I need to declare an array of pointers to functions like so: extern void function1(void); extern void function2(void); ... void (*MESSAGE_HANDLERS[])(void) = { function1, function2, ... }; However, I want the the array to be declared as constant -- both the data in the array and the pointer to the data. Unfortunately, I do n...

Convert std::string to const char* or char*

How can I convert an std::string to a char* or a const char*? ...

Defining const values in C

I have a C project where all code is organized in *.c/*.h file pairs, and I need to define a constant value in one file, which will be however also be used in other files. How should I declare and define this value? Should it be as static const ... in the *.h file? As extern const ... in the *.h file and defined in the *.c file? In what...

Why must const members be intialized in the constructor initializer rather than in its body?

Why must class members declared as const be initialized in the constructor initializer list rather than in the constructor body? What is the difference between the two? ...

Using elements of a constant array as cases in a switch statement

I'm attempting to map a set of key presses to a set of commands. Because I process the commands from several places, I'd like to set up a layer of abstraction between the keys and the commands so that if I change the underlying key mappings, I don't have to change very much code. My current attempt looks like this: // input.h enum LOG...

An operator == whose parameters are non-const references

I this post, I've seen this: class MonitorObjectString: public MonitorObject { // some other declarations friend inline bool operator==(/*const*/ MonitorObjectString& lhs, /*const*/ MonitorObjectString& rhs) { return lhs.fVal==rhs.fVal; } } Before we can continue, THIS IS VERY IMPORTANT:...

Can't assign a member which is a pointer to a templatized class

My problem is that in my "Widget" class i've the following declaration: MouseEvent* X; In a member function I initialize the pointer with an address the normal way: X = new MouseEvent; Ok, this last line makes the compiler stop at: error C2166: l-value specifies const object All right, a MouseEvent is declared as a typedef t...

Calling a const function from a non-const object

Hi, I need to call a const function from a non-const object. See example struct IProcess { virtual bool doSomeWork() const = 0L; }; class Foo : public IProcess { virtual bool doSomeWork() const { ... } }; class Bar { public: const IProcess& getProcess() const {return ...;} IProcess& getProcess() {return ...;} ...

Why Can't I Have "public static const string S = "STUFF"; In My Class

When trying to compile my class I get an error: The constant 'NamespaceName.ClassName.CONST_NAME' cannot be marked static. at the line: public static const string CONST_NAME = "blah"; I could do this all of the time in Java. What am I doing wrong? And why doesn't it let me do this? ...

Can I initialize a const string from a const char in C#?

I am trying to do the following in a way or another: const char EscapeChar = '\\'; const string EscapeString = EscapeChar.ToString(); // or ("" + EscapeChar) This does't compile. Is there another way to make it work? ...

Is there a difference between private const and private readonly variables in C#?

Is there a difference between having a private const variable or a private static readonly variable in C# (other than having to assign the const a compile-time expression)? Since they are both private, there is no linking with other libraries. So would it make any difference? Can it make a performance difference for example? Interned st...

Function-level constants - declare at top of function?

I have a constant value that I only plan to use once in my codebase. I am going to declare it using a const declaration. If this was a function-level variable I would declare it at the point of usage, but doing so with a constant just seems to clutter my function. ...

Why can I change the values of a const char* variable?

Why does the following code in C work? const char* str = NULL; str = "test"; str = "test2"; Since str is a pointer to a constant character, why are we allowed to assign it different string literals? Further, how can we protect str from being modified? It seems like this could be a problem if, for example, we later assigned str to a ...