The usage of const
Part I: const variable
const is a complie time constraint that an object can not be modified.
const int i = 0;
i = 9 ; // error
const int *pi = &i; // data is const, pointer is not const.
p1++; // complies, runs ok
int* const p2; //pointer is const, data is not const
const int* const p3; // data and pointer are both const
if const is on the left of the *, data is const;
if const is on the right of the *, pointer is const.
so const int *p4 = &i; equals to int const *p4 = &i;
switch between the const and non-const.
const int i = 9;
const_cast<int&>(i) = 6; // i is modified from const to non-const.
int j;
static_cast<const int&>(j) = 7;//j is modified from non-const to const.

Part II const with functions

const parameters: const int & a can keep the a from changing.
this is useful for we puts i to function and can keep i from chaning inside the function.
so void setAge(const int a) {age = a;} // is not useful , for it takes a copy of i as input, and we does not
care about the copy is const or non-const anyway.

例子:
const type & function_max (const type_B & a)
const type &: makes sure function returns a reference, not needs to copy;
const type_B & a; make sure this function will not change the contents of the a;



this can be overloaded, for example:
void setAge(const int & a) {age = a;}
void setAge(int &a) {age = a;}
so which one will be called,
when calling the function, since here "a" is a lvalue reference, if main function is :
d.setAge(40); first one is called;
i = 40; d.setAge(i); //second one is called
const return value ;
const string getName() {return name;} // here the const does not make any sense, for return "name" is a copy of the name, declare it as const to copy does not make any sense.
int main() {
Dog d;
const string& n = d.getName();
}
const function (a function that inside the function, it can not change the function variables)

void printDogName() const {cout << name << "const" << endl; age ++ } // it will error out
void printDogName() const {cout << getName() << "const" << endl; age ++ } // it will also error out,
// for a const function can only call a const function
const function can be reload
void printDogName() const {cout << name << "const" << endl; age ++ }
void printDogName() {cout << name << "non-const" << endl; age ++ }
so when object is non-const, first one will be called;
when the object is const, second one will be called.
Dog d;
d.printDogName(); //call first one
const Dog d2;
d2.printDogName(); // call second one
d.printDogName();
When a function is declared as const, it can be called on any type of object. Non-const functions can only be called by non-const objects.
For example the following program has compiler errors.
https://www.geeksforgeeks.org/const-member-functions-c/

(the getvalue is not constant, so it can only be called by non-constrant functions)

(the function is const, so the object calling it can be const or non const)
an object declared as const cannot be modified and hence, can invoke only const member functions as these functions ensure not to modify the object.
A const object can be created by prefixing the const keyword to the object declaration. Any attempt to change the data member of const objects results in a compile-time error.
浙公网安备 33010602011771号