const

How many and which are the uses of "const" in C++?

Hi, as a novice C++ programmer there are some constructs that look still very obscure to me, one of these is const. You can use it in so many places and with so many different effects that is nearly impossible for a beginner to come out alive. Will some C++ guru explain once forever the various uses and whether and/or why not to use them...

Advantages of const in C++?

What are the advantages of const in C++ (and C) for the uninitiated? exact duplicate of http://stackoverflow.com/questions/455518/how-many-and-which-are-the-uses-of-const-in-c I beg to differ - while there are uses for const, the discussion is why use it. The question specified would be an interesting and substantive link, though. ...

How to put values to const char **?

If I declare a variable const char ** stringTable, how should I be able to put values to it if it is a const? (It has to be const because a function I am supposed to use takes const char ** as a parameter.) Edit: No you cannot convert from char ** to const char ** implicitly. Compiler complains: cannot convert parameter 3 from 'char **'...

Difference between const declarations in C++

What is the difference between void func(const Class *myClass) and void func(Class *const myClass) See also: http://stackoverflow.com/questions/269882/c-const-question http://stackoverflow.com/questions/455518/how-many-and-which-are-the-uses-of-const-in-c and probably others... ...

Returning a reference from a constant function

#include "iostream" #include "vector" class ABC { }; class VecTest { std::vector<ABC> vec; public: std::vector<ABC> & getVec() const { //Here it errors out return vec; } }; Removing the const fixes it , is it not the case that getVec is a constant method. So why is this not allowed? ...

Passing "const" variable to method in Java

Is there an equivalent in Java to the passing on const references in C++? Isn't leaving out the "constness" misleading in regard to the method signature? ...

Compiler Error with const function

I am not sure whether I am missing something basic. But I am unable to understand why the compiler is generating the error for this code: class A { }; class B { public: B(); A* get() const; private: A* m_p; }; B::B() { m_p = new A; } A* B::get() const { //This is compiling fine return m_p; } class C { public...

Why doesn't C# offer constness akin to C++?

References in C# are quite similar to those on C++, except that they are garbage collected. Why is it then so difficult for the C# compiler to support the following: Members functions marked const. References to data types (other than string) marked const, through which only const member functions can be called ? I believe it would...

C# - Is this declared string treated as a const?

Help me settle an argument here. Is this: SqlCommand cmd = new SqlCommand( "sql cmd", conn); treated exactly the same as this: const string s = "sql cmd"; SqlCommand cmd = new SqlCommand( s, conn); Ie. does it make a difference if I state specifically that the string s is a const. And, if it is not treated in the same way, why n...

Problems with const set&. Compiler/STL bug or non-portable usage?

Are there any language lawyers in the house? Should the following code compile? include <set> bool fn( const std::set<int>& rSet ) { if ( rSet.find( 42 ) != rSet.end() ) return true; return false; } On one of the platforms (Sun Workshop) this does not compile. It reports that the find function returned an iterator and the end fu...

Designing a lazy vector: problem with const

I wrote a little "lazy vector" class (or, delayed vector) which is supposed to look like a std::vector and usable wherever a std::vector is used, but it loads its elements "lazily", i.e. it will load element n (and possibly a few more) from disk whenever someone accesses element n. (The reason is that in my app, not all elements fit into...

An odd C++ error: test.cpp:15: error: passing ‘const *’ as ‘this’ argument of ‘*’ discards qualifiers

Hi all, I'm having some trouble with a particular piece of code, if anyone can enlighten me on this matter it would be greatly appreciated, I've isolated the problem down in the following sample: #include <iostream> using namespace std; class testing{ int test(); int test1(const testing& test2); }; int testing::test(){ retur...

When, if ever, should we use const?

Const is baked into the client code. Readonly isn't. But const is faster. May be only slightly though. The question is, is there ever any scenario where you should prefer const over readonly? Or to rephrase, are we not practically always better off using a readonly instead of a const (keeping in mind the above-said baking thing)? ...

Returning a void pointer to a constant object in C

I'm writing an access function which returns a pointer to an internal buffer and I'd like to hint to users of my function that they shouldn't update the object that's pointed to. A very contrived example would be: void myAccessFunc(bool string, void* p, size_t* len) { static const char* s = "aha!"; static const int i = 123; if (...

c/c++ changing the value of a const

I had an article, but i lost it, it showed and described a couple of c/c++ tricks that ppl should be careful. One of them interested me but now that i am trying to replicate it I'm not being able to put it to compile. The concept was that it is possible to change by accident the value of a const in c/c++ It was something like this: co...

Assigning to const int *

void main() { const int * a; *a = 5; } gcc error : assignment of read only location. so, how to assign to *a, without using another variable? and what could be a use of a declaration like above? ...

const vs enum in D

Check out this quote from here, towards the bottom of the page. (I believe the quoted comment about consts apply to invariants as well) Enumerations differ from consts in that they do not consume any space in the final outputted object/library/executable, whereas consts do. So apparently value1 will bloat the executable, while va...

C++ best way to define cross-file constants

Hi, I am working on a game and have an interesting question. I have some game-wide constant values that I want to implement in one file. Right now I have something like this: constants.cpp extern const int BEGINNING_HEALTH = 10; extern const int BEGINNING_MANA = 5; constants.hpp extern const int BEGINNING_HEALTH; extern const int B...

What are the requirements for C++ template parameters?

If you are using a template in C++ that takes an integer value as a parameter, are there any requirements on an integer variable used as the parameter that are different than if the variable was used as a parameter in a function call? This is a follow-up to question here . I specifically want to address if there is a difference WRT v...

How to pass references by value in C++?

I'm trying to create am immutable type (class) in C++, I made it so that all methods "aka member functions" don't modify the object and return a new instance instead. I'm running across a bunch of issues, but they all revolve around the reference types in C++. One example is when passing parameters of the same class type by reference:...