一.引用 引用是别名 必须在定义引用时进行初始化.初始化是指明引用指向哪个对象的唯一方法. const 引用是指向 const 对象的引用: ; const int &refVal = ival; // ok: both reference and object are const int &ref2 = ival; // error: non const reference to a const object 可以读取但不能修改 refVal ,因此,任何对 refVal 的赋值都是不合
在C++中可以声明const引用 const Type& name = var: const引用让变量拥有只读属性 const int &a = b const int &a = 10; Case1: <pre name="code" class="cpp">#include <iostream> using namespace std; //常引用的知识架构 int main() { //普通引用 int a = 1
1.const指针 eg: (1) int const * p = nullptr; p = new int[10]; p[3] = 4; //error 我们发现第三行没法编译,这是因为第一行的const限制的时int,意思就是无法改变p所指的int类型的值. (2) int * const p = nullptr; p = new int [10]; //error 这是为什么呢?原来时因为const限定的是int*,也就是限定的是指向int*的指针,也就是p指针:所以第二行是报错的.因为
先简单回忆一下常量的性质: int main() { const int buffSize = 512; buffsize = 512; //× buffSize是常量 } 初始化时: const int i = get_val(); //√ 运行时初始化 const int j = 42; //√ 编译时初始化 const int k; //× k未经初始化 当用一个对象去初始化另外一个对象,他们是不是const就无关紧要 int i = 42; const int ci = i; int j
工程在window下编译没有任何问题, 但是在linux(CentOS6)下编译就老是报错 C++ 编译器已升级到最新版 6.1.0 错误如下: In file included /bits/stl_algobase.h::, /bits/char_traits.h:, /, , : /usr/local/include/c++//bits/stl_pair.h: In instantiation of ‘struct std::pair<const std::__cxx11::basic_st