假设Student类里有
char* a ="Tom";
char* b ="Rose";
如果有
Student* p;
那么
cout << p->a; // 输出Tom
cout << *(p->a); //输出T
这是为什么呢? a不是一个指针吗,如果是cout << p->a; 不是应该会输出a的地址吗
为什么cout << *(p->a); 只是输出T呢?
PHP中文网2017-04-17 12:09:24
A variable representing a string is essentially a pointer. An expression has two meanings. The compiler must have a certain logic to handle this situation, otherwise it will be a mess
In fact, the implementation logic of the I/O class in the C++ standard library when overloading <<操作符
is: when encountering char类型指针
, it will be treated as a string and the character pointed to by the pointer will be output. string.
To output the address, force conversion to 无类型的指针
cout << static_cast<const void *>(p->a);
*(p->a)Why is T
For a char类型指针
value*
operation, of course, it is to obtain the value of a char