ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

QT乱翻书-const

2021-11-07 03:32:00  阅读:217  来源: 互联网

标签:const 常量 符号 int 翻书 test01 变量 QT


跳转到大纲

 

QT乱翻书-const

 1 #include <iostream>
 2 #include <string.h>
 3 
 4 using namespace std;
 5 
 6 
 7 /*
 8 C中的const可以通过指针进行修改
 9 C++中的const优化:
10 1、如果以常量初始化const,编译器会将变量的值放入符号常量表
11 通过指针变量p访问空间内容*p取的是空间的值;
12 通过变量a访问的是符号常量表中的值。
13 2、如果以变量初始化const修饰的变量,没有符号常量表,立即开辟空间。
14 5、尽量用const代替#define,const有类型,const有作用域
15 
16 */
17 
18 void test01()
19 {
20     const int a = 10; // 放入了符号常量表
21     int *p = (int *)&a;
22     *p = 100;
23     cout << "*p = " << *p << endl;
24     cout << "a = " << a << endl;
25 }
26 void test02()
27 {
28     int b = 100;
29     const int a = b; // 放入了符号常量表
30     int *p = (int *)&a;
31     *p = 100;
32     cout << "*p = " << *p << endl;
33     cout << "a = " << a << endl;
34 }
35 
36 struct stu {
37     int num;
38     char name[32];
39 };
40 
41 void test03()
42 {
43     stu lucy = {100, "lucy"};
44     stu *p = (stu *)&lucy;
45     p->num  = 300;
46     strcpy(p->name, "bobo");
47     cout << p->num << ":" << p->name;
48 }
49 
50 int main()
51 {
52     test01();
53     test02();
54     test03();
55     return 0;
56 }

 

标签:const,常量,符号,int,翻书,test01,变量,QT
来源: https://www.cnblogs.com/kanbuxiaqu/p/15518957.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有