C++ 还提供了另外两种指针转换:指向任意数据类型的指针都可转换为
void* 类型;整型数值常量 0 可转换为任意指针类型。
显式转换也称为强制类型转换(cast),包括以下列名字命名的强制类型转换操
作符:static_cast、dynamic_cast、const_cast和 reinterpret_cast。
因为要覆盖通常的标准转换,所以需显式使用强制类型转换。
ival *= static_cast<int>(dval); // converts dval to int
显式使用强制类型转换的另一个原因是:可能存在多种转换时,需要选择一
种特定的类型转换。
函数不能返回另一个函数或者内置数组类型,但可以返回指向函数的指针,或指
向数组元素的指针的指针
尽管函数的形参是 const,但是编译器却将 fcn 的定义视
为其形参被声明为普通的 int 型:
void fcn(const int i) { /* fcn can read but not write to i*/ }
void fcn(int i) { /* ... */ } // error: redefines fcn(int)
这种用法是为了支持对 C 语言的兼容,因为在 C 语言中,具有 const 形
参或非 const 形参的函数并无区别。
三种定义是等价的,形参类型都是 int*
// three equivalent definitions of printValues
void printValues(int*) { /* ... */ }
void printValues(int[]) { /* ... */ }
void printValues(int[10]) { /* ... */ }