const int x = 123; // 定义在全局作用域上,只能用于此文件
extern const int x = 123; // 可以用于其他的文件
引用
int ival = 1024;
int &refVal = ival; // 一定要初始化,并且只能是object,并且引用以后不能改成引用另一个object
const引用:指向const object的引用
const int ival = 1024; // const type不能被非const引用
const int &refVal = ival; //不能修改
也可以:
const int &r = 2;
const int &r2 = r + i
typedef double wages;
enum open_modes {input, output, append};
//input == 0, output == 1, append == 2
enum open_modes {input = 1, output, append};
//input == 1, output == 2, append == 3
enum open_modes {input = 1, output, append = 2};
//input == 1, output == 2, append == 2
class xxxx{
}; //;别忘了...
struct xxxx{
//前面的都是public的,不要写public:了。。。
private:
...
};
头文件:无(变量和函数定义)可以有(inline函数,定义类, 值在编译时已知的const object)
避免多重包含:
#ifndef xxoo
#define xxoo
//define
#endif
string.size()返回string::size_type (只知道是unsigned)
string[index]可以做left value
vector: 类模板
vector<int>: 数据类型
v.push_back(t)
vector<int>::size_type // correct
vector::size_type // incorrect
for (vector<int>::size_type ix = 0; ix != v.size(); ++ix)
v[ix] = 0;
优先使用!=而不是<
v.size()不保存具体值:可能会动态增长
v.size(): inline函数
v[ix]不能添加不存在的新element(不自动申内存空间)
vector<int>::iterator iter = v.begin();
*iter访问元素
vector<int>::const_iterator iter;
*iter返回const值, no赋值
const vector<int>::iterator iter = v.begin();
iter不能++等……但是可以改变*iter
改变vector长度以后,不能信赖之前指向vector的iterator了!
#include<bitset>
bitset<n> b;
bitset<16> b(0xffff);
string strval("1100")
bitset<32> b(strval);
b.size() 返回 size_t 类型
int *ip1, *ip2;
int* 不是一个数据类型
void*指针可以存 任意类型object address
* 比 + 优先级高
int * = &ia[2];
p[1] : *(p + 1): ia[3]
p[-2]: *(p - 2): ia[0]
用const void* 保存 const object
func传值parameter一般定义为 指向const的指针,确保不在函数体内修改
int *pia = new int[10];
int *pia2 = new int[10] (); // 编译器安排初始化成0
delete [] pia;
int (*ip)[4]; // 指向含有4个element数组的pointer
int *ip[4]; // 四个指针数组
++i; // 返回加完以后的值
i++; // 返回加之前的值
(*p).foo == p->foo
new 返回分配内存的第一个地址
static_cast<int>()
const_cast<char*>(p) // p之前是 const char *p,之后把const性质去掉了;只能改变const性质,+,-
const_cast<里面只能用pointer, reference, 成员指针>
switch: 只能在最后一个case:或者default:后面定义变量,但可以在{}内定义
for initialization里多个定义只能有一种类型(只有一个语句)
function parameter list里parameter避免复制:
void f(const string &s1) {}
引用不复制+const不修改引用值
不用修改的para都定义成 const引用
传vector一般传它的iterator
传数组:
void f(int*)
void f(int[])
void f(int[10])
三个其实都是传的int*进去,并且compiler没有检查数组长度是否相符和
void f(const int*) // 不会修改数组里元素
引用传参数:
void f(int (&arr)[10]) //大小为10的数组的引用;int &arr[10]: 10个int引用
compiler会检查大小
int *matrix[10]; //10个pointer的数组
int (*matrix)[10]; // 10个int的数组的pointer
传多维数组:
void f(int (*matrix)[10], int rowSize)
或 void f(int matrix[][10], int rowSize)
如果对于程序prog,执行时: prog -o asdf
argv[0] = "prog"
argv[1] = "-o" ...
不要返回局部对象的引用
可以引用返回left value:
char &get_val() {}
或const char &get_val() {}
在函数声明中确定 默认实参(只能定义一次)
只能放最后面
static local object
inline 函数 放头文件
class A里成员function:
声明: int foo() const; // const表示this的type是:const A *const this
定义同样要有const
class A constructor:
A(): member_a("initial value a"), member_b("initial value b") {}
overloaded function 不能仅仅是 返回值不同
bool (*foo)(const string&)
foo: 指向函数的指针
bool *foo(const string&)
foo: 返回值为bool*的函数
调用方式:
foo(x)
以及 (*foo)(x)都可以
函数parameter为 函数指针:
void foo(const string &, bool(const string &));
或 void foo(const string &, bool (*)(const string &));
返回值为 函数指针:
int (*ff(int))(int*, int)
等价于
typedef int (*PF)(int*, int);
PF ff(int);