nullptr
The new C ++ 11 added to initialize nullptr a null pointer. nullptr is a special type of literals, it can be converted to any other type of pointer. Now we should try to use nullptr instead of NULL to initialize a null pointer.
int *a = nullptr;
char *c = nullptr;
Declarations and definitions of variables
Variables can be defined and only once, but can be declared more than once.
extern double pi = 3.14; // 定义
int main(void)
{
extern int a; // 声明
int b; // 定义
extern double f = 0.01; // 错误:不允许对外部变量的局部声明使用初始值设定项(初始化)
}
About references
Reference itself is not an object, you must initialize the definition of reference, and can not be changed once defined after its point.
int a = 0;
int b = 1;
int &ref = a;
ref = b; // 该语句其实等价于a=b,并非将ref改成b的引用,引用一旦定义就无法改变其指向。
And a pointer to a pointer Constants
Cptr code type is a pointer const double pointer to a pointer that is constant. So * cptr can not be changed, but cptr itself can be changed, since pi is not const double, so a direct change pi is legal.
double pi = 3.14;
const double *cptr = π
pi = 3.1; // 合法
cptr = 0; // 合法
*cptr = 0; // 非法
Pip code point is a pointer const const double type, i.e. constant pointer constant. * pip pip itself and are not modifiable. CurrErr is constant while the pointer, but it can not be modified currErr modified value of currErr pointed errNumb.
int errNumb = 0;
int *const currErr = &errNumb;
const double pi = 3.14;
const double *const pip = π
constexpr constant expression
C ++ 11 predetermined, allowing the variable declared as constexpr whether the type of a variable to be verified by a compiler is a constant value expression. Variables declared constexpr must be a constant, and must be initialized with a constant expression:
constexpr int mf = 20;
constexpr int limit = mf + 1;
constexpr int sz = size(); // 非法,只有当size()是一个constexpr函数时正确。
If the constexpr statement defines a pointer, the pointer qualifier constexpr only effective, regardless of the pointed-to object.
This type of code p and q far, p is a pointer to a constant, q is a constant pointer.
const int *p = nullptr;
constexpr int *q = nullptr;
With the address of the variable constexpr modified pointer must be a constant, such as placing in the global memory area const int i, if const int i = 0 on the functions that the sentence will be given, because it is the address of the stack i on, it will change the value of p will not be able to become a constant expression.
const int i = 0;
int main(void)
{
constexpr const int *p = &i;
}
Type alias
In C ++ 11 new alias to a method of the type using. wages and wages2 and double are equivalent.
typedef double wages;
using wages2 = double;
wages d1;
wages2 d2;
To note that the following case, cstr cstr2 and data types are not the same, is a pointer const char cstr2 object pointer, it is to char cstr object pointer constant. We should deal with the pstring as a whole rather than go back to his alias replacement process.
typedef char *pstring;
const pstring cstr = 0;
const pstring *ps;
const char *cstr2 = 0;
decltype
decltype by inference to infer the type of the expression values of variable types to be defined, the following should be particularly noted that the code corresponds * p type but not int & int.
const int ci = 0, &cj = ci;
decltype(ci) x = 0;
decltype(cj) y = x;
decltype(cj) z; //错误:z是一个引用,必须初始化
int i = 42, *p = &i, &r = i;
decltype(r + 0) b; //正确,b是int类型
decltype(*p) c; // 错误,*p是解引用,其得到的数据类型是引用型,必须初始化。可以理解为*p是i的别名,所以*p也是引用类型。
auto d = *p; // 正确,d是int型,要和上面的区分开来。
decltype((i)) e; // 错误,(i)会让编译器认为是一个表达式,因此是int&类型,必须初始化。