2. const member functions #

Created Saturday 11 January 2020

What is const in OOP? #

How to make a function ‘const’ #

class A
{
	...
	void foo() const
	{
		// code
	}
	...
};

Note

  1. Const functions can be invoked through both const and non const objects.
  2. Const objects can access only const functions, i.e const objects are limited in their actions.
  3. A const function can change attributes for a non-const param passed to it. The only restriction is changing the caller object.
  4. Special functions(like constructors, destructor and copy assignment operator) cannot be made const.
  5. A function can be overloaded to have a const function and a non-const, both are considered different, because constness is part of the function signature. Here a const object will call the const function, otherwise the normal function is called.

How does const work #

const functions just have a** const T* instead of a normal this. **

Changing a data structures in a const function #

Why use as constant functions?: https://en.wikibooks.org/wiki/C%2B%2B_Programming/Programming_Languages/C%2B%2B/Code/Keywords/return

https://stackoverflow.com