C++ const总结
来源:互联网 发布:用文言文说网络流行句 编辑:程序博客网 时间:2024/06/06 15:03
2.4.2const对象默认为文件的局部变量
在全局作用域里定义非const变量时, 它在整个程序中都是可以访问的
除非特别说明, 在全局作用域声明的const变量时定义该对象的文件的局部变量static. 此变量只存在于那个文件中, 不能被其他文件访问
想要const变量能够在其他文件中被访问, 必须显示地指定它为extern.
file1.cc
extern const int bufsize = fcn(); //定义外部可用
file2.cc
extern const int bufsize; //声明是外部的
2.9.1 const定义的位置
1.定义在头文件中
一些const对象可以定义在头文件中
当const是通过常量表达式自我初始化的, 他就可以被编译器看见, 这样的const对象可以放在头文件中.
当const变量是用常量表达式初始化时, 可以保证所有的变量都有相同的值.但是在实践中,大部分的编译器在编译是都会用相应的常量表达式来替换对这些const变量的使用.所以,在实践中不会有任何存储空间用于存储用常量表达式初始化的const变量.
file.h
const int a = 10;
2.定义在源文件中
如果const变量不用常量表达式初始化,那么它就不应该在头文件中定义.相反,和其它变量一样, 该const变量应该在一个源文件中定义并初始化.然后再头文件中为他添加extern声明, 以使其能被多个文件共享.
file.cc
extern const int ci =fcn();
int main()
{
int a;
cin >> a;
const int b = a;
return 0;
}
4.2.5 const限定符和指针
1.指向const对象的指针 //可以修改指针指向不同的对象, 但是不能通过修改指针修改指针指向的实体
const double di = 9.999;
double dj =7.7777;
const double *cptrdi; /*不需要初始化*/
const double *cptrdj;
cptrdi = &di;
cptrdj = &dj; /* 指向的对象可以是const的, 也可以不是*/
被称为"自以为指向const的指针".
PS:
当对象是const时, 不能使用void*指正保存const对象的地址, 必须要用const void*类型的指针保存const对象的地址:
const int universe = 42;
const void *cpv = &universe; //ok
void *pv = &universe; //error
PPS:
实际程序中, 指向const的指针常用作函数的形参, 就是我们要用到这个指针但是又不想不小心更改了指针指向的值的时候, 这样就能避免实参不会因为在函数中因为形参被修改.
2.const指针
指针本身不能修改, 需要在定义时初始化赋值, 之后就不能修改了.
int errNumb = 9;
int errType = 10;
int *const curErr = &errNumb;
*curErr = 11; //ok
curErr = &errType; //error
3.指向const对象的const指针
const double cpi = 3.1415926;
const double *const pi_ptr = π //继承了需要初始化的特点
同样继承了不需要一定指向的是const类型的变量的特点, 如下也行
double pi = 3.1415926;
const double *const cpi_cptr = π
综上:
#include <iostream>
using namespace std;
int main()
{
int i = -1;
const int ci = i;
const int *pci = &ci;
int *const cpi = &ci; //error, ci是cosnt的实体, 但是cpi指向的实体不是const类型的
const int *const cpci = ⁣
return 0;
}
4.指针和typedef
/*typedef不是文本扩展*/
string s;
typedef string * pstring;
const pstring cstr = &s;
等价于
string * const cstr = &s; //因为声明const pstring时, const修饰的是pstring的类型, 这是一个指针, 类似于const int a = 10;这样子
所以如下三种是等价的, 都是说明这是const指针, 指针所指向的值是不能改变的.
string s;
typedef string *pstring;
const pstring cstr1 = &s;
pstring const cstr1 = &s;
string *const cstr3 = &s;
5.const和引用
由于引用一旦定义不可更改, 所以const引用是指向const对象的引用
const int ival = 1024;
const int &refval = ival;
refval = 100; //error
可以读取但是不能修改refval, 任何对refval的赋值都是不合法的
#include <stdio.h>
#include <iostream>
using namespace std;
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
int j = 10;
const int ci = 10;
int &qj2 = j;
// int &qci2 = ci; //error: invalid initialization of reference of type 'int&' from expression of type 'const int'
const int &qj = j; //const引用可以指向非const变量, 但是同样不能使用该引用修改变量
const int &qci = ci;
qj2 = 99;
// qj = 99; //error: assignment of read-only reference `qj'
return 0;
}
6.const放在类型前面是修饰类型的, 代表这是一个const的类型, 发在变量前面也是修饰类型的, 同一行的变量都是const.
引用和普通类型一样, 同一行的变量都是const的.
但是指针前得const就不是了, 指针前的只修饰这个指针.
/*const int * p 和 int const *p是一样的, 修饰的是类型int, 不是指针.*/
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
int i = -1;
const int ci = i, ci1 = i;
// ci1 = 10; //error: assignment of read-only variable `ci1
int const ci11 = i, ci12 = i;
// ci12 = 10; //error: assignment of read-only variable `ci12'
const int ci2 = i;
const int *pci = &ci2, *pci1 = &ci2;
// *pci1 = 10; //error: assignment of read-only location
int ci21 = i;
const int *pci11 = &ci21, *pci12 = &ci21;
// *pci12 = 10; //error: assignment of read-only location
int j = 2;
int k = 3;
int *const cpj = &j, *cpk = &j;
// cpj = &k; //error: assignment of read-only variable `cpj'
cpk = &k; //ok
int const * cpj1 = &j, *cpk1 = &j;
cpj1 = &k;
cpk1 = &k; //ok
int m = 9;
const int cm = 10;
const int &qci = i, &qci2 = m;
// qci2 = 99; //error: assignment of read-only reference `qci2'
const int &qci21 = ci, &qci22 = cm;
// qci22 = 99; //error: assignment of read-only reference `qci22'
return 0;
}
- C++const用法总结
- C++const用法总结
- C++const用法总结
- C++const用法总结
- C++const用法总结
- C++const用法总结
- C++const用法总结
- c++->const用法总结
- C++const总结
- C++:const用法总结
- C++const用法总结
- C++const总结
- C++-const 总结
- C/C++ const总结
- <C++> const总结
- const 用法总结(C++)
- C语言Const用法总结
- C语言总结之const
- 关于JAVA线程池
- Jquery Cookie切换皮肤
- OFBiz端口修改
- JS牛人博客
- vs2008 NONCLIENTMETRICS 错误
- C++ const总结
- 如何从优化的角度去选择CMS
- WHAT IS ISO? A CAMERA’S SENSITIVITY TO LIGHT EXPLAINED, AND HOW TO USE IT
- java.lang.OutOfMemoryError: Java heap space 解决方法
- Halcon 之 Hello World
- tomcat数据库连接池配置
- IOS7完美越狱(iphone5s,5c)
- C++中CRT检测内存泄露
- 黑马基础测试题整理