const

Const-correctness and immutable allocated objects

During a recent discussion (see comments to this answer), R.. recommended to never create aliases for pointer-to-const types as you won't be able to deallocate the referenced objects easily in a conforming C program (remember: free() takes a non-const pointer argument and C99 6.3.2.3 only allows conversions from non-qualified to qualifi...

Problem with const qualifiers to get private atributes of an object

I'm a completely new to C++ and I'm having a very stupid problem. I have a Graph class and I need to create a copy constructor for it. This is my class: #include <igraph.h> #include <iostream> using namespace std; class Graph { public: Graph(int N); // contructor ~Graph(); // destructor Graph(const Graph& other); // Copy ...

Question on reuse of pointers after passing to functions

So, when I pass a const char * to a function once, can I use it again? It appears to end up spitting out crap to me. const char *config_file = "file.txt"; function(int x, config_file); cout << "Number" << x; secondfunction(int y, config_file); Do I need to make another pointer to config_file? If so, how do I do that? Thanks! ...

How to (deep)copy a map from a const object

Hello, I have another problem I can't seem to solve..., or find on this site... I have an object (called DataObject) with a map, declared as follows: std::map<size_t, DataElement*> dataElements; Now i have a copy function (used in the copy constructor): void DataObject::copy(DataObject const &other) { //here some code to clean...

Call a non-const member function from a const member function

I would like to know if its possible to call a non-const member function from a const member function. In the example below First gives a compiler error. I understand why it gives an error, I would like to know if there is a way to work around it. class Foo { const int& First() const { return Second(); } int& Secon...

Why doesn't PHP support curly-brace expansion of consts in a string?

PHP supports this: $z = 5; $str = "z is $z"; // result: "z is 5" and it supports this: $c = new StdClass(); $c->x = 9; $str = "x is {$c->x}"; // result: "x is 9" but it does NOT support this: class abc { const n = 2; } $str = "x is {abc::n}"; // result: "x is {abc::n}" Why does PHP not support insertion of consts via the c...

Question regarding const qualifier and constructor

Here is simple program. If I comment constructor, I get an error Just wanted to check what is the reason for this? t.cc: In function 'int main(int, char**)': t.cc:26: error: uninitialized const 'const_test' #in...

How to name a constant in Objective-C?

What's the naming convention for constants in Objective-C (or most widely used way to name them)? Is there a different criteria for extern constants? Some styles I have seen: NSString* const kPreferenceFirstRun = @"FirstRun"; // Replace "XY" by a prefix representing your company, project or module NSString* const XYPreferenceFirstRu...

c++ 'const' at end of member function

what exactly does the 'const' keyword in c++ mean when it's written at the end of a member function (after the argument list)? thanks! ...

C++ "const" keyword explanation

When reading tutorials and code written in C++, I often stumble over the const keyword. I see that it is used like the following: const int x = 5; I know that this means that x is a constant variable and probably stored in read-only memory. But what are void myfunc( const char x ); and int myfunc( ) const; ? ...

Strange error, set<int>::begin() always returning const iterator

Why is set.begin() always returning a const iterator and not a standard one? 35 int test(){ 36 std::set<int> myset; 37 myset.insert(2); 38 myset.insert(3); 39 int &res = *myset.begin(); 40 return res; 41 } test.cpp:39: error: invalid initialization of reference of type ‘int&’ from expression of type ‘const int’ ...