C++学习笔记之---控制变化的const
来源:互联网 发布:网络主持人特点 编辑:程序博客网 时间:2024/05/18 01:10
//const与基本数据类型//const与指针类型#include <iostream>using namespace std;int main(){const int x = 10;//x = 20; 此处会报错!!!const修饰其值改变不了return 0;}int main(){//1.const int *p = NULL; 与 int const *p = NULL等价int x = 3, y = 4;const int *p = &x;p = &y;//此处正确//*p = 4;此处为错误的//2.int *const p = NULL;int *const p = &x;//p = &y; 此处报错//3、const int * cont p = NULL;const int *const p = &x;//此处改变不了的return 0;}
例子:
#include <iostream>using namespace std;int main(){const int x = 3;x = 5;int x = 3;const int y = x;y = 5;int x = 3;const int * y = &x;*y = 5;int x = 3, z = 4;int *const y = &x;y = &z;const int x = 3;const int &y = x;y = &z;return 0;}
结果如图:
具体请查看错误信息:
代码如下:
#include <iostream>using namespace std;int main(){const int x = 3;x = 5;return 0;}
结果:
#include <iostream>using namespace std;int main(){int x = 2;int y = 5;int const *p = &x;cout<<*p<<endl;p = &y;cout<<*p<<endl;return 0;}
#include <iostream>using namespace std;int main(){int x = 2;int y = 5;int const &z = x;z = 10; //会报错x = 11;return 0;}
//函数使用const
//函数使用const#include <iostream>using namespace std;void fun(int &a, int &b){a = 10;b = 22;}//函数有问题//不能赋值/*void fun1(const int &a, const int &b){a = 33;b = 44;}*/int main(){int x = 2;int y = 5;fun(x, y);cout<<"函数没有const修饰的结果是: "<< x <<" , "<< y <<endl;/*int v = 3;int w = 4;fun1(v, w);cout<<"函数没有const修饰的结果是: "<<v<<" , "<<w<<endl;*/return 0;}
如果上例代码的注释去掉就会出现如下错误信息:
0 0
- C++学习笔记之---控制变化的const
- C/C++学习笔记之const的学习
- c语言学习笔记之const关键字(理解)
- C/C++学习笔记7:const运用(2)之const成员函数
- C++学习笔记之const
- C++学习笔记之const
- 学习笔记2-C++-const static 关键词的用法
- Java学习笔记之线程的控制
- 《C++ primer》学习笔记之十一:const的放置位置
- c++学习笔记之函数重载与const的关系
- 我的小小学习笔记(2)之C语言——浅谈关键字 const,static,volatile(1)
- 黑马程序员-------c语言学习笔记之流程控制
- c语言学习笔记之格式控制详细
- C++之离港篇学习笔记之const
- C学习笔记6 CONST用法
- C语言笔记之const关键字
- iOS学习笔记之关键词const
- ES6学习笔记之《let 和 const》
- Matlab优化问题08—fminimax
- centos7下使用yum安装mysql
- ASP.NET sync over async
- leetcode 198:House Robber
- Xcode5.1和IOS8.3真机调试
- C++学习笔记之---控制变化的const
- [corefx注释说]-System.Collections.Generic.Queue<T>
- android悬浮窗口的实现
- 如何安装vmware tools,实现vmware里linux和windows文件共享
- [异能程序员]第二章 上头条(第二更)
- 江尤理:做单犹豫?散户执行力不强请离开白银市场
- leetcode 70:Climbing Stairs
- Android手机安全卫士(一)---概述、启动、签名、主界面、自定义控件
- mac 原来使用了trim enable 到了 10.10.4开启trimforce 后出现开机禁止符号解决办法