春日 - 朱熹
胜日寻芳泗水滨,无边光景一时新。
等闲识得东风面,万紫千红总是春。
const 是 constant 的缩写,本意是不变的,不易改变的意思。
#include
using namespace std;
const int MAX_VALUE = 100;
void main()
{
// MAX_VALUE = 50; // 常量的值不可被修改,定义时就确定好了
system("pause");
}
code:
#include
using namespace std;
int main()
{
int a = 10;
int b = 20;
const int* p = &a;
cout << "a的地址: " << &a << endl;
cout << "p的值: " << p << endl;
// *p = 20; // 错误,const修饰的是*p,则*p的值不可修改,即p指向的内容不可改
p = &b; // p的指向是可以修改的
cout << "b的地址: " << &b << endl;
cout << "p的值: " << p << endl;
system("pause");
return 0;
}
result:
a的地址: 00000001930FF5C4
p的值: 00000001930FF5C4
b的地址: 00000001930FF5E4
p的值: 00000001930FF5E4
code:
#include
using namespace std;
int main()
{
int a = 10;
int b = 20;
int* const p = &a; // 指针常量,const修饰p,则p的指向不可修改
cout << "a的地址: " << &a << endl;
cout << "p的值: " << p << endl;
// p = &b; // 错误,p的指向不可修改
*p = 20; // ,const修饰的是*p,则*p的值不可修改,即p指向的内容不可改
//p = &b; // p的指向是不可以修改的
cout << "a的值: " << a << endl;
system("pause");
return 0;
}
result:
a的地址: 000000AB945FF994
p的值: 000000AB945FF994
a的值: 20
code:
#include
using namespace std;
int main(void)
{
int i = 1;
const int& cj = i; // const引用普通变量
cout << "------ 1 ------, " << cj << endl;
//cj=2; // 错误cj被const修饰,不可以改变,无法通过引用改变它引用的内容
i = 3; // 正确,i可以被修改,cj也随之被更改
cout << "------ 2 ------, " << cj << endl;
system("pause");
return 0;
}
result:
------ 1 ------, 1
------ 2 ------, 3
code:
#include
#include
using namespace std;
void print_str(const string s) // 值传递,形参和实参并不是同一空间,
// 这里加不加const都不会影响实参的值
{
//s = "jh"; // 会报错,s无法修改
cout << s << endl;
}
int main()
{
string str1 = "hello world";
print_str(str1);
system("pause");
return 0;
}
result:
hello world
#include
using namespace std;
void func(const int &a) // 在调用函数的时候,t1传给a,则a为t1的引用,而const修饰了a,那么a是无法改变的,t1也就无法改变
{
cout << a << endl;
//++a; //是错误的,a 不能被改变
}
int main(void)
{
int t1 = 10;
func(t1);
system("pause");
return 0;
}
const修饰函数返回值,表示返回值不可被改变。
// 以下效果一样
const int func1(int a);
int func1(int a);
如果给以“指针传递”方式的函数返回值加 const 修饰,那么函数返回值(即指针)的内容不能被修改,该返回值只能被赋给加 const 修饰的同类型指针;
const char * GetString(void); // 返回值是常量指针
//如下语句将出现编译错误:
//char *str = GetString();
//正确的用法是
const char *str = GetString(); // 返回是常量指针,则只能赋值给常量指针
用 const 修饰返回的引用,保护引用的内容不被修改。
code:
#include
using namespace std;
class Person {
public:
int& get_age()
{
cout << "m_age的地址: " << & m_age << endl;
return m_age;
}
const int& get_age_const() // const int &,则无法通过返回的引用改变m_age的值
{
return m_age;
}
void show_age()
{
cout << "age: " << m_age << endl;
}
private:
int m_age = 0;
};
int main()
{
Person p1;
p1.show_age();
cout << "p1.get_age()的地址: " << &p1.get_age() << endl;
p1.get_age() = 5; // p1.get_age()的返回值是m_age的引用,
//这句话就是改变m_age的值,会修改成员变量的值
p1.show_age();
//p1.get_age_const() = 8; // 编译器会报错,因为p1.get_age_const()返回的是
// const int&, 常量引用,无法通过引用改变变量的值。
//p1.show_age();
system("pause");
return 0;
}
result:
age: 0
m_age的地址: 0000002D1F3BF734
p1.get_age()的地址: 0000002D1F3BF734
m_age的地址: 0000002D1F3BF734
age: 5
code:
#include
using namespace std;
class Horse
{
public:
int age = 3;
mutable string color = "white";
//this 指针是一个指针常量, Horse * const this, 它指向的地址不可以修改, const Horse * const this, 则表示其空间的内容也不能修改
void show_age() const //常函数,const其实是用来修饰this指针的,表示this指向的内存空间的内容也不可以修改
{
//age = 5; // 常函数中不能修改普通成员变量
color = "black"; // 当成员变量被mutable修饰后,常函数中可以修改
cout << "it is " << age << endl;
}
};
int main()
{
Horse horse1;
horse1.show_age();
system("pause");
return 0;
}
result:
it is 3
code:
#include
using namespace std;
class Horse
{
public:
int age = 3;
mutable string color = "white";
//this 指针是一个指针常量, Horse * const this, 它指向的地址不可以修改, const Horse * const this, 则表示其空间的内容也不能修改
void show_info() const //常函数,const其实是用来修饰this指针的,表示this指向的内存空间的内容也不可以修改
{
//age = 5; // 常函数中不能修改普通成员变量
color = "black"; // 当成员变量被mutable修饰后,常函数中可以修改
cout << "it is " << age << endl;
}
void show_info_1()
{
//age = 5;
color = "black";
cout << "it is " << age << endl;
}
};
int main()
{
Horse horse1;
horse1.show_info();
const Horse horse2; // 常对象内的内容不可以修改
//horse2.age = 5; // 常对象不能修改普通的成员变量
horse2.color = "brown"; // 常对象可以修改mutable修饰的成员变量
cout << horse2.age << endl;
cout << horse2.color << endl;
horse2.show_info();
//horse2.show_info_1(); // 常对象不能调用普通的成员函数,因为普通成员函数可以修改成员变量的值,而常对象对应的成员变量是不可以修改的,冲突。
system("pause");
return 0;
}
result:
it is 3
3
brown
it is 3