代码示例
void f(int) { }
void f(void*) { }
int main(int argc, char* argv[])
{
// C++2.0以前初始分为小括号、中括号、大括号都可以初始化
string str("initialize");
int iarr[2] = { 1, 2 };
// C++2.0以后统一使用这样的方式初始化
int x { 100 }; // initialize value
int arr[] {1, 2, 3}; // initialize value
string lstr{ "POWERFUL" };
vector<string> data { "CAT", "DOG", "MOUSE" }; // initialize value
// C++2.0以后新增了auto,自动类型判断,根据表达式右边的值自动判断类型
// auto的设计初衷是为了解决类型过于复杂或对类型记忆模糊的时候使用
auto *p = &data; // auto keyword
auto it = p->begin();
// C++2.0以前只有NULL,表示数据指向为一个NULL的内存模块
//其实#define NULL 0是这样定义的
//如果有重载函数f(int)和f(void*),使用NULL有歧义,两个都可以
//为了解决这样的问题,C++2.0新增了nullptr明确是一个指针类型
f(0);
f(nullptr); // nullptr replace NULL
cout << "x:" << x << endl;
cout << "arr[1]:" << *(arr + 1) << endl;
cout << "data[1]:" << data.at(1) << endl;
}