5.4const对象

常成员函数

一个const对象可以调用const函数,但不能调用非const成员函数。必须将关键字const放在函数参数表之后,才能说明该函数是一个const成员函数。

声明常成员函数的格式如下:

类型标识符 函数名(参数列表) const;

定义定义如下:

类型标识符 类名::函数名(参数列表) const {//函数体}

在类中定义定义如下:

类型标识符 函数名(参数列表) const {//函数体}

在常成员函数里,不能更新对象的数据成员,也不能调用该类中没有用const修饰的成员函数。如果将一个对象说明为常对象,则通过该对象只能调用它的const成员函数,不能调用其他成员函数。

//error: C3490: 由于正在通过常量对象访问“x”,因此无法对其进行修改

//error: C3490: 由于正在通过常量对象访问“y”,因此无法对其进行修改

加上const属性,不可修改一般的成员变量

1 void Dialog::getxy() const
2 {
3     this->x=900;//error: C3490: 由于正在通过常量对象访问“x”,因此无法对其进行修改
4     this->y=600;//error: C3490: 由于正在通过常量对象访问“y”,因此无法对其进行修改
5 }

常量数据成员一旦初始化以后,不会读内存,从代码区的符号表自动生成。

//error C2789: “myclass::coint”: 必须初始化常量限定类型的对象

//error C2530: “myclass::myint”: 必须初始化引用

 1 #include <iostream>
 2 
 3 class myclass
 4 {
 5 public:
 6     int num;
 7     int data;
 8     int *p;
 9 
10     //error C2789: “myclass::coint”: 必须初始化常量限定类型的对象
11     const int coint;//常量数据成员
12 
13     //error C2530: “myclass::myint”: 必须初始化引用
14     int & myint;//引用
15 
16     static int shu;//静态数据成员
17 public:
18     static void go()
19     {
20 
21     }
22     void run()
23     {
24 
25     }
26     myclass(int, int);//构造函数,常量限定类型的对象、引用必须重载构造函数初始化
27     ~myclass()
28     {
29 
30     }
31 };
32 
33 int myclass::shu = 0;//初始化静态数据成员
34 
35 myclass::myclass(int a, int b) :myint(a), coint(b)//构造函数
36 {
37 
38 }
39 
40 void main()
41 {
42 
43     system("pause");
44 }

//引用就是共用地址,常量新开辟备份机制

//常量const在类的外部,一开始必须初始化,在代码区

//常量const在类的内部,先构建一个类的对象才能初始化,在栈或者堆

//类中的普通成员变量,类名::变量名,在栈上

//类名 *指针名=new 类名,在堆上

//类的静态成员变量,在静态区

//函数都在代码区,类的函数、静态函数都是共享的

//引用本质就是变量的别名,4个字节,本质是指针

 1 #include <iostream>
 2 
 3 class myclass
 4 {
 5 public:
 6     int num;
 7     int data;
 8     int *p;
 9 
10     //error C2789: “myclass::coint”: 必须初始化常量限定类型的对象
11     const int coint;//常量数据成员
12 
13     //error C2530: “myclass::myint”: 必须初始化引用
14     int & myint;//引用
15 
16     static int shu;//静态数据成员
17 public:
18     static void go()//静态成员函数
19     {
20 
21     }
22     void run()
23     {
24 
25     }
26     myclass(int, int);//构造函数,常量限定类型的对象、引用必须重载构造函数初始化
27     ~myclass()
28     {
29 
30     }
31 };
32 
33 int myclass::shu = 0;//初始化静态数据成员
34 
35 myclass::myclass(int a, int b) :myint(a), coint(b)//构造函数
36 {
37     //引用就是共用地址,常量新开辟备份机制
38     //常量const在类的外部,一开始必须初始化,在代码区
39     //常量const在类的内部,先构建一个类的对象才能初始化,在栈或者堆
40     std::cout << &a << " " << &b << std::endl;
41     std::cout << &myint << " " << &coint << std::endl;
42 }
43 
44 //类中的普通成员变量,类名::变量名,在栈上
45 //类名 *指针名=new 类名,在堆上
46 //类的静态成员变量,在静态区
47 //函数都在代码区,类的函数、静态函数都是共享的
48 
49 //引用本质就是变量的别名,4个字节,本质是指针
50 
51 void main()
52 {
53     myclass myclass1(10, 9);//初始化一个对象
54 
55     system("pause");
56 }

//静态常数据成员在静态区

static const int dashu;//静态常数据成员

const int myclass::dashu = 20;//初始化静态常数据成员

 1 #include <iostream>
 2 
 3 class myclass
 4 {
 5 public:
 6     int num;
 7     int data;
 8     int *p;
 9 
10     //error C2789: “myclass::coint”: 必须初始化常量限定类型的对象
11     const int coint;//常量数据成员
12 
13     //error C2530: “myclass::myint”: 必须初始化引用
14     int & myint;//引用
15 
16     static int shu;//静态数据成员
17 
18     //静态常数据成员在静态区
19     static const int dashu;//静态常数据成员
20 public:
21     static void go()//静态成员函数
22     {
23 
24     }
25     void run()
26     {
27 
28     }
29     myclass(int, int);//构造函数,常量限定类型的对象、引用必须重载构造函数初始化
30     ~myclass()
31     {
32 
33     }
34 };
35 
36 int myclass::shu = 0;//初始化静态数据成员
37 const int myclass::dashu = 20;//初始化静态常数据成员
38 
39 myclass::myclass(int a, int b) :myint(a), coint(b)//构造函数
40 {
41 
42 }
43 
44 void main()
45 {
46 
47     system("pause");
48 }

常对象

常成员函数

一个const对象可以调用const函数,但不能调用非const成员函数。

//mutable是不受const约束的类成员

//常对象不可以引用非const成员函数

//error C2662: “void area::add(int)”: 不能将“this”指针从“const area”转换为“area &”

 1 #include <iostream>
 2 
 3 class area
 4 {
 5 public:
 6     int x;
 7     int y;
 8     mutable int z;//mutable是不受const约束的类成员
 9     area() :x(10), y(10)
10     {
11 
12     }
13     void printxy() const//常成员函数
14     {
15         std::cout << x << " " << y << " " << z << std::endl;
16     }
17     void add(int a)//非const成员函数
18     {
19         x += a;
20         y += a;
21     }
22     void go()//非const成员函数
23     {
24 
25     }
26 protected:
27 private:
28 };
29 
30 void main()
31 {
32     const area area1;//常对象不可以引用非const成员函数
33 
34     area1.printxy();
35 
36     area1.add(1);//error C2662: “void area::add(int)”: 不能将“this”指针从“const area”转换为“area &”
37 
38     area1.go();//error C2662: “void area::go(void)”: 不能将“this”指针从“const area”转换为“area &”
39 
40     system("pause");
41 }
原文地址:https://www.cnblogs.com/denggelin/p/5658672.html