const

How do you avoid code duplication when implementing const and non-const iterators?

I'm implementing a custom container with an STL-like interface. I have to provide a regular iterator and a const iterator. Most of the code for the two versions of the iterators is identical . How can I avoid this duplication? For example, my container class is Foo, and I'm implementating FooIterator and FooConstIterator. Both of th...

Does Java have a const reference equivalent?

Here's a snippet of code: //Game board is made up of Squares. A player can place GamePieces on a Square. public class CheckersBoard { public boolean PlaceGamePiece(GamePiece gamePiece, int nRow, int nColumn) { return m_theGameBoard[nRow][nColumn].PlaceGamePiece(gamePiece); } private Squa...

Double const declaration

Hi. I often see the following function declaration: some_func(const unsigned char * const buffer) { } Any idea why the const is repeated before the pointer name? Thanks. ...

Using 'const' in class's functions

I've seen a lot of uses of the const keyword put after functions in classes, so i wanted to know what was it about. I read up smth at here: http://duramecho.com/ComputerInformation/WhyHowCppConst.html . It says that const is used because the function "can attempt to alter any member variables in the object" . If this is true, then shoul...

const cast to allow read lock, does this smell bad ?

I want to execute a read-only method on an object marked as const, but in order to do this thread-safely, I need to lock a readers-writer mutex: const Value Object::list() const { ScopedRead lock(children_); ... } But this breaks because the compiler complains about "children_" being const and such. I went up to the ScopedRead cla...

Static variable of procedure in Delphi

What is difference in memory management of variables a and b? Are they both similar static variables but visibility of b is local? Is it ok to declare static variable in procedure or function? const a: string = 'aaa'; procedure SubMethod; const b: string = 'bbb'; begin a := a + 'a'; b := b + 'b'; end; ...

Return a const reference or a copy in a getter function?

What's better as default, to return a copy (1) or a reference (2) from a getter function? class foo { public: std::string str () { // (1) return str_; } const std::string& str () { // (2) return str_; } private: std::string str_; }; I know 2) could be faster but don't have to due to (N)RVO. 1) is ...

gcc optimization, const static object, and restrict

I'm working on an embedded project and I'm trying add more structure to some of the code, which use macros to optimize access to registers for USARTs. I'd like to organize preprocessor #define'd register addresses into const structures. If I define the structs as compound literals in a macro and pass them to inline'd functions, gcc has...

[C++] Mixing extern and const

Can I mix extern and const, as extern const? If yes, does the const qualifier impose it's reign only within the scope it's declared in or should it exactly match the declaration of the translational unit it's declared in? I.e. can I declare say extern const int i; even when the actual i is not a const and vice versa? ...

How do I require const_iterator semantics in a template function signature?

I am creating a constructor that will take a pair of input iterators. I want the method signature to have compile-time const semantics similar to: DataObject::DataObject(const char *begin, const char *end) However, I can't find any examples of this. For example, my STL implementation's range constructor for vector is defined as: tem...

How can I define two global `const` variables to the same value in a C module?

I’m using a pair of global variables in one of my .c files, matched to a single extern declaration each in two different .h files (well, one .h file, preprocessed two ways). One is for public consumption, and one is for private use. Both are const variables. I only want to initialize one of the variables in my .c file, and then assign t...

Swap method with const members

Hi everyone! I want to implement a Swap() method for my class (let's call it A) to make copy-and-swap operator=(). As far as I know, swap method should be implemented by swapping all members of the class, for example: class A { public: void swap(A& rhv) { std::swap(x, rhv.x); std::swap(y, rhv.y); std:...

Storing a global struct variable inside another global struct in C

I’m trying to figure out a way to use nested global structs as a sort of API namespacing for my C library. Specifically, I want to expose a single Primary ‘namespacing struct,’ that contains other such structs (such as Primary.Secondary), that themselves contain function pointers (Primary.Secondary.a_function()). I’ve abstracted out th...

Why does this work? Returning const references in C++

I am fooling around with C++ and const references and am confused why this code works: #include <iostream> class A { public: A() : a_(50) {} const int& getA() const { return a_; } private: const int a_; }; int main(int argc, char* argv[]) { A* a = new A(); const int& var = a->getA(); std::cout << var << std::en...

why isnt it legal to convert (pointer to pointer to non-const) to a (pointer to pointer to a const)

Hi, as its legal to convert a pointer to non-const to a pointer to a const.. similarly,why isnt it legal to convert (pointer to pointer to non-const) to a (pointer to pointer to a const) i.e, char *s1 = 0; const char *s2 = s1; // OK... char *a[MAX]; // aka char ** const char **ps = a; // error! Thanks. ...

How can I concatenate a constant and a variable and store it in a class constant with PHP?

class My_class { const STATUS_ERROR = 0; const STATUS_OK = 1; const DB_TABLE = TABLE_PREFIX . 'class_table'; } The two status consts work fine and can be accessed within class methods as self::STATUS_ERROR and self::STATUS_OK just fine. The issue is one of how to stop the following error being thrown when I try to define th...

C++ const : how come the compiler doesn't give a warning/error

Really simple question about C++ constness. So I was reading this post, then I tried out this code: int some_num = 5; const int* some_num_ptr = &some_num; How come the compiler doesn't give an error or at least a warning? The way I read the statement above, it says: Create a pointer that points to a constant integer But some_num ...

Constant correctness

In the printMessage if you access the vector of a constant class using the index it works fine, but not with the the iterator (*itr). If the iterator is declared as constant_iterator then it works fine. Why? In both cases I am reading the data and not modifying the vector. Can someone shed some light? #include <iostream> #inclu...

C++ typedef interpretation of const pointers

Firstly, sample codes: Case 1: typedef char* CHARS; typedef CHARS const CPTR; // constant pointer to chars Textually replacing CHARS becomes: typedef char* const CPTR; // still a constant pointer to chars Case 2: typedef char* CHARS; typedef const CHARS CPTR; // constant pointer to chars Textually replacing CHARS becom...

Working with constant and non-constant functions - C++

I have 3 classes. In it's simplest form, it looks like, class tree { public: tree_node* find_node(const std::string& text) { return factory.find(text); } private: tree_node_factory factory; } class tree_node { public: tree_node(const std::string& text) : text_(text) {} const std::string& text() const { ...