Tutorial by Examples

Declaration and usage. // a is const int, so it can't be changed const int a = 15; a = 12; // Error: can't assign new value to const variable a += 1; // Error: can't assign new value to const variable Binding of references and pointers int &b = a; // Error: ca...
int a = 0, b = 2; const int* pA = &a; // pointer-to-const. `a` can't be changed through this int* const pB = &a; // const pointer. `a` can be changed, but this pointer can't. const int* const pC = &a; // const pointer-to-const. //Error: Cannot assign to a const reference *pA = b...
Member functions of a class can be declared const, which tells the compiler and future readers that this function will not modify the object: class MyClass { private: int myInt_; public: int myInt() const { return myInt_; } void setMyInt(int myInt) { myInt_ = myInt; } }; In a ...
In C++ methods that differs only by const qualifier can be overloaded. Sometimes there may be a need of two versions of getter that return a reference to some member. Let Foo be a class, that has two methods that perform identical operations and returns a reference to an object of type Bar: class ...

Page 1 of 1