以下是测试代码:
修改前:
int sssd = 33; int *pint = &sssd; int *volatile* vppint = &pint; cout << "vppint = &pint(不相等): " << &pint << endl; cout << "vppint = &pint(不相等): " << vppint << endl;
输出:
如果我们想把该地址打印出来也是可以的,有两种方式:(1)因为 cout << 似乎没有对 volatile int* 做重载运算符的操作,因此没有重载输出运算符函数 operator << 的函数参数与 volatile int* 相匹配。所以默认如果变量值大于 0 输出值为 1 (即 true),如果小于 0 输出值为 0 (即 false),默认以 bool 型输出。
(2)之所以被转成了 bool 类型是因为指针可以转换为 bool。空指针为 false、其它值为 true;因为bool 默认的输出方式是值,所有输出的值是1,我们其实可以通过 std 中的 boolalpha (把 bool 解析为 true 或 false)打印一下
(注:c++中例如函数指针也是没有对应的的输出函数 std::basic_ostream::operator<< 输出时也会遇到这种情况的)
- 使用 C 语言中的 printf 函数输出打印,C++ 中使用输出流对象 cout 打印不了,是因为 C++ 中 cout 没有重载输出运算符函数 operator << 的函数参数与 volatile int* 相匹配。
- 把 volatile int* 类型强转为 void * 类型,因为对象 cout 的输出运算符中没有 volatile int* 类型导致输出不了,所有我们可以把它这个类型强转为它认识的类型就可以正常输出了
修改后:
int sssd = 33; int *pint = &sssd; int *volatile* vppint = &pint; cout << "vppint = &pint: " << &pint << endl; cout << "vppint = &pint: " << (void*)vppint << endl;
输出:



