const-correctness

Why can't I convert 'char**' to a 'const char* const*' in C?

The following code snippet (correctly) gives a warning in C and an error in C++ (using gcc & g++ respectively, tested with versions 3.4.5 and 4.2.1; MSVC does not seem to care): char **a; const char** b = a; I can understand and accept this. The C++ solution to this problem is to change b to be a const char * const *, which disallows ...

"const correctness" in C#

I'm a heavy C++ user who dabbles in C# in his spare time. I'm also one of those const-correctness nazis and so not being able to do this easily in C# grates a little. The point of const-correctness is to be able to provide a view of an instance that can't be altered or deleted by the user. The compiler supports this by pointing out wh...

How to achieve const-correctness in C#?

Hi all, I have programmed C++ for many years but am fairly new to C#. While learning C# I found that the use of the const keyword is much more limited than in C++. AFAIK, there is, for example, no way to declare arguments to a function const. I feel uncomfortable with the idea that I may make inadvertent changes to my function argument...

Sell me on using const correctness

So why exactly is it that it's always recommended to use const as often as possible? It seems to me that using const can be more of a pain than a help in C++. But then again, I'm coming at this from the python perspective: if you don't want something to be changed, don't change it. So with that said, here are a few questions: It se...

Why is my return type meaningless?

I am trying to use a return type of const MyClass * const. However, I get a warning: Warning: #815-D: type qualifier on return type is meaningless. Is this not a valid type? I want a pointer than cannot be changed, and I want the thing it points to to not be changed either. ...

Const-correct Notifier in Observer Pattern

Hi, I want to implement an Observer of a Model class which does not change the Model. Thus, it should be able to use a const-Reference to access the Model. But the Registering of the Observer prohibits this. Here is how the observer pattern is implemented in my Project: //Attributes of type Observable are used by classes that want ...

How does const correctness help write better programs?

This question is from a C# guy asking the C++ people. (I know a fair bit of C but only have some general knowledge of C++). Allot of C++ developers that come to C# say they miss const correctness, which seems rather strange to me. In .Net in order to disallow changing of things you need to create immutable objects or objects with read o...

const pointers in STL containers

Hello fellow C++ programmers. I have, what I hope to be, a quick question about STL containers: std::list<std::string> l; This statement compiles fine when used in some C++ sourcefile (with the appropriate includes). But std::list<const std::string> m; or std::list<std::string * const> n; fails to compile when using gcc (gcc ver...

How to generate a non-const method from a const method?

While striving for const-correctness, I often find myself writing code such as this class Bar; class Foo { public: const Bar* bar() const { /* code that gets a Bar somewhere */ } Bar* bar() { return const_cast< Bar* >( static_cast< const Foo* >(this)->bar()); } }; for lots of methods like bar(). Writing these non-con...

Why is const-correctness specific to C++?

Disclaimer: I am aware that there are two questions about the usefulness of const-correctness, however, none discussed how const-correctness is necessary in C++ as opposed to other programming languages. Also, I am not satisfied with the answers provided to these questions. I've used a few programming languages now, and one thing that b...

Confused on const correctness with static array of pointers to const objects

I'm still not sure I totally get how this particular case should work out. So if I want to declare an array of NSStrings that won't change, is this correct? static NSString * const strings[] = {@"String 1", @"String 2", ...}; Is the static necessary? (what does it do?) Am I missing an extra const somewhere? There's just too many pl...

How to deal with initialization of non-const member in const object?

Let's say you have a class class C { int * i; public: C(int * v):i(v) {}; void method() const; //this method does not change i void method(); //this method changes i } Now you may want to define const instance of this class const int * k = whatever; const C c1(k); //this will fail but this will fail be...

Const correctness for value parameters

I know there are few question about const correctness where it is stated that the declaration of a function and its definition do not need to agree for value parameters. This is because the constness of a value parameter only matters inside the function. This is fine: // header int func(int i); // cpp int func(const int i) { return...

Is this code legal in C++

I just found that when it comes to templates this code compiles in g++ 3.4.2 and works unless m() is not called: template <typename T> class C { T e; public: C(): e(0) {}; void m() { e = 0; }; }; Now one may create and use instance C<const int> c; Until c.m() is not called there are no compile errors b...

Is "const LPVOID" equivalent to "void * const"?

And if so, why some Win32 headers use it? For instance: BOOL APIENTRY VerQueryValueA( const LPVOID pBlock, LPSTR lpSubBlock, LPVOID * lplpBuffer, PUINT puLen ); A bit more elaboration: If the API never uses references (or any other C++-only constructs) but only pointers and values, what is the point of having const L...

Const correctness: const char const * const GetName const (//stuff);

Labelled as homework because this was a question on a midterm I wrote that I don't understand the answer to. I was asked to explain the purpose of each const in the following statement: const char const * const GetName() const { return m_name; }; So, what is the explanation for each of these consts? ...

Can a heap-allocated object be const in C++?

In C++ a stack-allocated object can be declared const: const Class object; after that trying to call a non-const method on such object is undefined behaviour: const_cast<Class*>( &object )->NonConstMethod(); //UB Can a heap-allocated object be const with the same consequences? I mean is it possible that the following: const Class*...

about const member function

I met two explanation of const member function class A{ public: ... void f() const {} ... } it means it could only access constant members; it means it does not modify any members; I think the second one is right. But why does the first one come out? Is there anything to be clarify? Thanks! ...

How do I require const_iterator semantics in a template function signature?

I am creating a constructor that will take a pair of input iterators. I want the method signature to have compile-time const semantics similar to: DataObject::DataObject(const char *begin, const char *end) However, I can't find any examples of this. For example, my STL implementation's range constructor for vector is defined as: tem...

Is it a good practice to free memory via a pointer-to-const

There are many questions discussing the details of C and C++ dealing with pointer-to-const deletion, namely that free() does not accept them and that delete and delete[] do and that constness doesn't prevent object destruction. What I am interested on is whether you think it is a good practice to do so, not what the languages (C and C++...