const

Does const help the optimizer? C++

Possible Duplicate: Constants and compiler optimization in C++ Let the holy wars begin: I've heard a number of differing opinions on the usefulness of const in C++. Of course it has uses in member function declarations, etc. But how useful is it as a modifier on variables (or rather, constants)? Does it indeed help the optimiz...

Is const_cast acceptable when defining an array?

I have a static const array class member (const pointers to SDL_Surfaces, but that's irrelevant), and have to loop through it in order to populate it. Aside from a const_cast when I'm done looping, which I hear is bad practice, how would I go about doing this? EDIT: The reason I don't just do... static SDL_Surface *const myArray[3]; ....

How can I initialize a const variable of a base class in a derived class' constructor in C++?

I have an abstract C++ class with no constructor. It's supposed to be a base class so other classes can inherit from it. What I am trying to do is to declare a constant variable in the base class and initialize it in each derived class' constructor but nowhere else in each one of those classes. Is it legal in C++? If so, how can I do tha...

c++ qualifier error

Hi All, I have begun writing some code for a library I need. The following code gives me an error class node { public: node() { } node(const node&); ~node() { } luint getID() { return this->ID; } node& operator=(const node&); protected: luint ID; std::vector<node*> neighbors; }; node::node( const node& inNod...

Difference between C++ const refernces and consts?

What is the difference between: const double& pi = 3.14; and (no ampersand): const double pi = 3.14; They both seem to have the same L and R values so what is the difference? Thanks. ...

Is there any use for a private constant, that is not static?

Is there any reason to have a private constant, that is not static? Are there any situations that you would need a non-static private const? Would it make sense if constants where static by default? I use ActionScript3 and some Java, but I think this is a broader OOP question. ...

How to design around lack of const in C#/Java?

While trying to model my domain, I came across the following problem. Let's image we have a Thing: class Thing { public int X { get; set; } } Things have a property X. Then, there are Packs, which aggregate Things. But the domain requires that there is some restriction on the Things the Packs can hold. Let it be for example that t...

const conflicting

Hello, I have the following class class node { public: node() { } node(const node&); node(luint inID) { ID = inID; } ~node() { neighbors.clear(); } node& operator=(const node&); void addNeighbor(luint); void addNeighbor(const std::vector<luint>& ); void setID(luint inID) { ID = inID; } luint getID() ...

C++ assign const to environment variable or default value

For an application that uses a number of environment variables, is there some kind of a convention or "best practice" when it comes to grabbing environment variables and putting them into either a struct or a bunch of const's? Obviously, I want to fallback to a default value for each and every environment variable. Right now, using the f...

calling boost::undirected_dfs from a constant context

I have a class that contains a BGL graph. I'd like to traverse the graph in a constant context. For example, I might want an accessor function to report if the graph is cyclic. As soon as I make my dfs function const the code won't compile. Here's a minimal example: #include <boost/graph/adjacency_list.hpp> #include <boost/graph/undir...

What's the correct way to use const in C++?

const correctness has me somewhat confused. What rule of thumb do you use to decide when something should be const or not? e.g. consider this example class MyClass { string ToString(); // this one? const string& ToString(); // or this? const string& ToString() const; // or this? char* ToString(); // What about this? const ...

cast Derived*const to Base*const

Edit - Put the question into context a bit more. Given: struct Base { ... }; struct Derived : public Base { ... }; class Alice { Alice(Base *const _a); ... }; class Bob : public Alice { Bob(Derived *const _a); ... }; When I try to implement Bob::Bob(Derived *const _d) : Alice(static_cast<Base*const>(_d)) { } ...

Overloading operator < with const, but don't insert into map as const

I'm having an issue. I have a class with an overloaded operator like this.. class Foo { friend bool operator<(const Foo &a, const Foo &b); ... }; bool operator<(const Foo &a, const Foo &b) { return a.member < b.member; } Then in a function in a class that holds some Foos in a map as keys... void Bar::Update()...

Error trying to make an wrapper of the STL map container

I'm trying to make a wrapper to the STL map container, in order to add a const method to return the value given the key. In map, operator[] isn't const, and find() requires dereferencing to get the value (map.find()->second). I'm basing some of my "research" off of http://stackoverflow.com/questions/152643/idiomatic-c-for-reading-from-a-...

Objective-C "const" question

Wikipedia says "const" is "...a special kind of variable whose value cannot typically be altered by the program during its execution..." If that is the case, why does this: const char *words[4] = { "aardvark", "abacus", "allude", "zygote" }; *words = "first"; words[1] = "second"; int wordCoun...

when to use const and const reference in function args

When writing a C++ function which has args that are being passed to it, from my understanding const should always be used if you can guarantuee that the object will not be changed or a const pointer if the pointer won't be changed. When else is this practice advised? When would you use a const reference and what are the advantages over...

Should I declare these methods const?

I'm working on some C++ code where I have several manager objects with private methods such as void NotifyFooUpdated(); which call the OnFooUpdated() method on the listeners of this object. Note that they don't modify the state of this object, so they could technically be made const methods, even though they typically modify the stat...

Why const is undefined in static function ?

Why the name constant is not recognised in the static function f2() ? class Foo { protected static function f1($s) { echo "doing $s"; } } class Bar extends Foo { const name = 'leo'; public static function f2() { Foo::f1(name); } } $bar = new Bar(); $bar->f2(); I get the following error: Notice: Use...

Const keyword appended to the end of a function definition... what does it do?

Suppose I define a function in C++ as follows: void foo(int &x) const { x = x+10; } And suppose I call it as follows: int x = 5; foo(x); Now typically (without the const keyword), this would successfully change the value of x from the caller's perspective since the variable is passed by reference. Does the const keyword change th...

What is the differnce between "const X a" and "X const a" if X is the class

i have a class name X, what is the difference between "const X a" and "X const a" ...