const

C++0x lambda capture by value always const?

Is there any way to capture by value, and make the captured value non-const? I have a library functor that I would like to capture & call a method that is non-const but should be. The following doesn't compile but making foo::operator() const fixes it. struct foo { bool operator () ( const bool & a ) { return a; } }; int _...

Returning a C++ reference in a const member functions

A have a class hierarchy that looks somethign like this: class AbstractDataType { public: virtual int getInfo() = 0; }; class DataType: public AbstractDataType { public: virtual int getInfo() { }; }; class Accessor { DataType data; public: const AbstractDataType& getData() const { return(data); } ...

Cast vector<T> to vector<const T>

I have a member variable of type vector<T> (where is T is a custom class, but it could be int as well.) I have a function from which I want to return a pointer to this vector, but I don't want the caller to be able to change the vector or it's items. So I want the return type to be const vector<const T>* None of the casting methods I tr...

gcc compiler error in copy ctor: "expected primary-expression before > token"

Here's my code. It compiles in VS2005 but not in gcc. Any ideas template<class T> Derived<T>::Derived(const Derived<T>& in) { Base<T>::Base<T>(in); //ERROR here } "expected primary-expression before > token" ...

ActionScript Defining a Static Constant Array

is it not possible to define a static const array? i would like to have an optional parameter to a function that is an array of colors, private static const DEFAULT_COLORS:Array = new Array(0x000000, 0xFFFFFF); public function myConstructor(colorsArray:Array = DEFAULT_COLORS) { } i know i can use ...args but i actually wanting to s...

What effect does static const have on a namespace member

// MyClass.h namespace MyNamespace { static const double GasConstant = 1.987; Class MyClass { // constructors, methods, etc. }; }; I previously had GasConstant declared within the MyClass declaration (and had a separate definition in the source file since C++ does not support const initialization of non-integral types). ...

Use of const double for intermediate results

Hi, I a writing a Simulation program and wondering if the use of const double is of any use when storing intermediate results. Consider this snippet: double DoSomeCalculation(const AcModel &model) { (...) const double V = model.GetVelocity(); const double m = model.GetMass(); const double cos_gamma = cos(model.GetFlightPath...

How to discard const in c++

This is what I'm trying to do and I can't: #include <string> using namespace std; class A { public: bool has() const { return get().length(); } string& get() { /* huge code here */ return s; } private: string s; }; The error I'm getting is: passing ‘const A’ as ‘this’ argument of ‘std::string& A::get()’ discards qualifiers I ...

Using a class with const data members in a vector

Given a class like this: class Foo { const int a; }; Is it possible to put that class in a vector? When I try, my compiler tells me it can't use the default assignment operator. I try to write my own, but googling around tells me that it's impossible to write an assignment operator for a class with const data members. One post I...

When does a const return type interfere with template instantiation?

From Herb Sutter's GotW #6 Return-by-value should normally be const for non-builtin return types. ... Note: Lakos (pg. 618) argues against returning const value, and notes that it is redundant for builtins anyway (for example, returning "const int"), which he notes may interfere with template instantiation. While Sutte...

Const parameter at constructor causes stackoverflow

I've found this strange behavior with VS2005 C++ compiler. Here is the situation: I cannot publish the code, but situation is very simple. Here is initial code: it work perfectly class Foo { public: Foo(Bar &bar) { ... } } The constructor implementation stores a reference, setup some members... indeed nothing special. If ...

Any problems with this C++ const reference accessor interface idiom?

I was converting a struct to a class so I could enforce a setter interface for my variables. I did not want to change all of the instances where the variable was read, though. So I converted this: struct foo_t { int x; float y; }; to this: class foo_t { int _x; float _y; public: foot_t() : x(_x), y(_y) { set(0, 0...

const correctness

I was going through: C++ FAQs about inheritance and decided to implement it (just to learn it) #include "Shape.h" void Shape::print() const { float a = this->area(); // area() is pure virtual ... } now, everything (well, almost) works as described in item: faq:23.1 except that print() is const and so it can't access th...

Why wasn't C# designed with 'const' for variables and methods?

Possible Duplicate: const correctness in C# I suspect const was simplified for the C# spec for general language simplicity. Was there a specific reason we can't declare variable references or methods as const like we can with C++? e.g.: const MyObject o = new MyObject(); // Want const cast referenece of MyObject o.SomeMetho...

C++ Constructor Parameters Question

Hi, I'm learning C++. I have a simple class named GameContext: class GameContext { public: GameContext(World world); virtual ~GameContext(); }; To initialize a GameContext object, I need a World object. Should the GameContext constructur take a pointer to a World object (World*), the address to a World object (&...

Exposing a std::list as read only

I have a class that contains, among other things, an std::list. I want to expose this list but only in such a way that the structure and the data it contains are read only, but still can be used with iterators. The way I've got it 'working' atm is to return a copy of the list. This leave my class 'safe' but of course does nothing to sto...

boost::shared_ptr<const T> to boost::shared_ptr<T>

I want to cast the const-ness out of a boost::shared_ptr, but I boost::const_pointer_cast is not the answer. boost::const_pointer_cast wants a const boost::shared_ptr, not a boost::shared_ptr. Let's forego the obligitory 'you shouldn't be doing that'. I know... but I need to do it... so what's the best/easiest way to do it? For clari...

consts and other animals

Hello i have a cpp code wich i'm having trouble reading. a class B is defined now, i understand the first two lines, but the rest isn't clear enough. is the line "B const * pa2 = pa1" defines a const variable of type class B? if so, what does the next line do? B a2(2); B *pa1 = new B(a2); B const * pa2 = pa1; B const * const pa3 = pa2;...

g++ linker can't find const member function

I have a Point class (with integer members x and y) that has a member function withinBounds that is declared like so: bool withinBounds(const Point&, const Point&) const; and defined like this: bool Point::withinBounds(const Point& TL, const Point& BR) const { if(x < TL.getX()) return false; if(x > BR.getX()) return false; ...

C++ const-reference semantics?

Consider the sample application below. It demonstrates what I would call a flawed class design. #include <iostream> using namespace std; struct B { B() : m_value(1) {} long m_value; }; struct A { const B& GetB() const { return m_B; } void Foo(const B &b) { // assert(this != &b); m_B.m_value += b.m_value; m_B.m_value += b...