const

why tr1::bind can not be compiled if type of argument is "const int"

#include <iostream> #include <vector> #include <algorithm> #include <iterator> #include <functional> #include <deque> using namespace std; #include <tr1/functional> using namespace std::tr1::placeholders; template<class Function_t> void for_each_x(Function_t func,int interval) { for(int sc = 0; sc < 10; sc+=interval){ func((con...

Is there a runtime benefit to using const local variables?

Outside of the ensuring that they cannot be changed (to the tune of a compiler error), does the JIT make any optimisations for const locals? Eg. public static int Main(string[] args) { const int timesToLoop = 50; for (int i=0; i<timesToLoop; i++) { // ... } } ...

Variably modified array at file scope

I want to create a constant static array to be used throughout my Objective-C implementation file similar to something like this at the top level of my ".m" file: static const int NUM_TYPES = 4; static int types[NUM_TYPES] = { 1, 2, 3, 4 }; I plan on using NUM_TYPES later on in the file so I wanted to put it in a variable. ...

Why doesn't gcc allow a const int as a case expression?

I was looking at this SO question and got to thinking about const ints versus #defines and realized I don't actually understand why the compiler can't deal with this. Could someone shed some light as to why the following code const int FOO = 10; int main(int argc, char** argv) { switch(argc) { case FOO: { printf("foo\n"...

C++ passing const string references in methods?

Hello, I'm trying to initialize a private variable of my Class passing a const string &aString to it as parameter. Here's my method: void Image::initWithTextureFile(const std::string &inTextureName) { Texture2D *imTexture = TEXTURE_MANAGER->createTexture(inTextureName); if(imTexture) { texture = imTexture; sca...

Member assignment in a const function

I have a class member myMember that is a myType pointer. I want to assign this member in a function that is declared as const. I'm doing as follows: void func() const { ... const_cast<myType*>(myMember) = new myType(); ... } Doing this works fine in VC++, but GCC gives an error with the message "lvalue required as left ...

C# - Is it possible to extend an existing built-in class with a new Interface

Hi, I am just learning C# and I have a problem now. :-) In C++ I loved to use "const reference" as a parameter to avoid that the called method changes my passed object. I read somewhere that I can do sth. similar in C# by using Interfaces. In the interface I would just put some "getters" to allow the method a readonly access to my obje...

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...

Const, conversion, and generic array problem

I have a problem in these lines: const int* index = faceArray[f].vertices; const Vector3& A = vertexArray[index[0]]; const Vector3& B = vertexArray[index[1]]; const Vector3& C = vertexArray[index[2]]; faceNormal[f] = Vector3::Cross(B - A, C - A).Normalize(); When I try to compile, I get an error: error C2678: binary '[' : no operat...

Const correctness in C++ operator overloading returns

Hi! I'm a little confused as to why I've been told to return const foo from a binary operator in c++ instead of just foo. I've been reading Bruce Eckel's "Thinking in C++", and in the chapter on operator overloading, he says that "by making the return value [of an over-loading binary operator] const, you state that only a const member f...

Do non const internal members of class become const when I have const ref to an object of that class?

Well, I think my problem originates in a lake of knowledge of basic C++ concepts. The problem is, in my code (below) I have the classes Header and Register. For both, I pass a reference to a ifstrem file already opened. The Header reads some bytes from it. Register has a method to return a reference of Header (which is passed in Register...

Location of const in a function

A similar question was previously asked, but none of the answers really provided what I was looking for. I am having trouble deciding where consts should be located in a function. I know a lot of people put them at the top, but if you put them as close as possible to where they are used, you'll reduce code span. I.e. void f() { const...

How to initialize static const pointer in a class ?

class School { static const int *classcapacity; }; This expression is from my exam and it need to get initialized how can i do that ? ...

Member function still const if it calls functions that break "constness"?

I'm wondering if a class's member function should be const if it calls other functions that modify its data members. A good example would be a public member function that uses private member functions to do the brunt of the work. void Foo::Show() const { // Doesn't directly modify data members in this function ShowPig(); // One or a...

Problems passing argument to a const parameter

Say I have a function that takes a const reference to a pointer... Example: void Foo( const Bar *&p_Thing, ); and I pass a pointer Bar *blah = NULL; // Initialized when program starts up to the function Foo( blah ); I may encounter a compiler error like this invalid initialization of reference of type 'const Bar*&' from expres...

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*...

Indirectly calling non-const function on a const object

Given the following code: class foo; foo* instance = NULL; class foo { public: explicit foo(int j) : i(j) { instance = this; } void inc() { ++i; } private: int i; }; Is the following using defined behavior? const foo f(0); int main() { instance->inc(); } I'm asking because I'm using a cl...

Any differences between f(const string &) and f(const string )?

class mystring { friend ostream& operator<<(ostream &out, const mystring ss) { out << ss.s; return out; } private: string s; public: mystring(const char ss[]) { cout << "constructing mystring : " << ss << endl; s = ss; } }; void outputStringByRef(const mystring &ss) { cout << "outputStri...

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! ...

Help loading contstants stored in serialized array using eval() and constant()

DISCLAIMER: Please read carefully as this is NOT a question about storing arrays in constants or simple eval() or serialize() techniques. This IS a question primarily about how constants work in PHP and why the constant() function is not working to convert a constant name into a constant value. Thanks. BACKGROUND: For various reasons, I...