class Address {
int i ;
char b;
string c;
public:
void showMap ( void ) ;
};
void Address :: showMap ( void ) {
cout << "address of int :" << &i << endl ;
cout << "address of char :" << &b << endl ;
cout << "address of string :" << &c << endl ;
}
输出是:
address of int : something
address of char : // nothing, blank area, that is nothing displayed
address of string : something
Why?
另一个有趣的事情:如果 int、char、string 是公共的,那么输出是
... int : something
... char :
... string : something_2
something_2 - something
始终等于 8。为什么? (不是 9)
当你获取 b 的地址时,你得到char *
. operator<<
将其解释为 C 字符串,并尝试打印字符序列而不是其地址。
try cout << "address of char :" << (void *) &b << endl
反而。
[编辑] 就像 Tomek 评论的那样,在这种情况下使用的更合适的转换是static_cast
,这是一个更安全的选择。下面是一个使用它而不是 C 风格转换的版本:
cout << "address of char :" << static_cast<void *>(&b) << endl;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)