OOP C5 Constant and Static
Constant
Const
const declares a variable to have a constant value.
Constants are variables observing scoping rules declared with const type modifier.
- A
constin C++ defaults to internal linkage. the compiler tries to avoid creating storage for aconstby holding the value in its symbol table. externforces storage to be allocated if neccessary.
variable, constant and literal
literaldoesn’t requires memory.constanttakes memory, keeps constant at most of time: free to read but decline to write after initialization. Obey the rule of variable.- Specially, complier will optimize the constant to a
literalif it could.
- Specially, complier will optimize the constant to a
variabletakes memory, life time and scope.
Compile time constants
1 | |
Value must be initialized unless a extern is given.
Run-time constants
1 | |
Aggregates
Storage will be allocated using const for aggregates. Here const means “a piece of storage that cannot be changed.”
However, the value cannot be used at compile time because the compiler is not
required to know the contents of the storage at compile time.
Pointers and const
-
e.g1
2
3
4
5
6
7
8
9char* const q = "abc";
*q = 'c'; //complier may not err,but memory of string literal is constant
char a[] = "abc";
char* const r = a;
*r = 'c';
r++; //err: q is const
const char* p = "abcd";
p++;
*p = 'e'; //err: (*p) is const
Three ways to add const to a pointer:
1 | |
*left toconst: the object is constant.*right toconst: the pointer is constant.
Assignment of const pointer
int i; |
const int ci = 3; |
|
|---|---|---|
int *ip; |
ip = &i; |
ip = &ci; ERR! |
const int* cip; |
cip = &i; |
cip = &ci; |
- always OK to assign a non-constant to a constant variable.
- Compliers don’t permit assign a pointer to
constto a pointer tonon-constin protection of modifying theconstvariable via the pointer tonon-const. - But compliers permit assign a pointer to
non-constto a pointer toconst, which may cause unexpected modification.
- Can always treat a non-constant value as constant, but cannot treat a constant object as non-constant without an
explicit_cast. - Passing a whole object may cost you a lot and it is better to pass by a pointer. Passing an address into a function and make it a
constto prevent modifying the original value occasionally.
constant in class
Const member functions
- Ensure no changes on members;
- Ensure no calls for
non-constfunctions;
1 | |
It is advised that make the function a const member function if it suits the standard.
ConstObejects
We could only call const member functions of a const object.
Overloaded const and non-const functions
1 | |
Actually the this parameter is different with a const, thus could be overloaded.
Constant in class
const member has to be initialized in initializer list of the constructor.
1 | |
enum
Static
Features
- Two basic meanings: Static storage
- allocated once at a fixed address
- Visibility of a name
- internal linkage
- In C++, don’t use
staticexcept inside functions and classes.
Usage
| type | meaning |
|---|---|
| static free function | internal linkage(deprecated) |
| static global variables | internal linkage(deprecated) |
| static local variables | persistent storage |
| static member variables | shared by instances |
| static member functions | shared by instances, can only access static members |