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 const in C++ defaults to internal linkage. the compiler tries to avoid creating storage for a const by holding the value in its symbol table.
  • extern forces storage to be allocated if neccessary.

variable, constant and literal

  • literal doesn’t requires memory.
  • constant takes 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 literal if it could.
  • variable takes memory, life time and scope.

Compile time constants

1
2
const int bufsize = 1024;
extern const int bufSize;

Value must be initialized unless a extern is given.

Run-time constants

1
2
3
4
5
6
const int class_size = 12; 
int finalGrade[class_size]; // ok
int x;
cin >> x;
const int size = x;
double classAverage[size]; // error!

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.g

    1
    2
    3
    4
    5
    6
    7
    8
    9
    char* 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
2
3
4
string p1("Fred"); 
const string* p = &p1;
string const* p = &p1;
string *const p = &p1;
  • * left to const: the object is constant.
  • * right to const : 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 const to a pointer to non-const in protection of modifying the const variable via the pointer to non-const.
  • But compliers permit assign a pointer to non-const to a pointer to const, 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 const to prevent modifying the original value occasionally.

constant in class

Const member functions
  • Ensure no changes on members;
  • Ensure no calls for non-const functions;
1
2
3
4
5
6
7
8
9
int Date::set_day(int d){ 
//...error check d here...
day = d;// ok, non-const so can modify
}
int Date::get_day() const {
day++; //ERROR modifies data member
set_day(12); // ERROR calls non-const member
return day; //ok
}

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
2
void f() const;
void f();

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
2
3
4
5
6
7
8
9
10
11
12
class HasArray { 
const int size;
int array[size]; // ERROR!
};
class HasArray {
enum { size = 100 };
int array[size]; // OK!
};
class HasArray {
static const int size = 100;
int array[size]; // OK!
}

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 static except 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

OOP C5 Constant and Static
http://example.com/2023/04/04/OOP-5/
Author
Tekhne Chen
Posted on
April 4, 2023
Licensed under