c++中string类型变量的地址为什么是变量的内容?

2025-03-23 08:52:08
推荐回答(1个)
回答1:

单步观测到的结果 有问题。
string str; 声明 str 是 string class.
string 不是普通的变量类型,而是 class ( 中文叫 “类”-- 不叫 “类型”)。
&str 在语句里 是 求 str 的地址。
语句里,str 是 class, 输出 "world" 要调用 成员函数 string 类的 c_str() 。
例如:
#include
#include
using namespace std;
main()
{
string str="world";
int i;
cout << &str << endl; // 输出 地址
cout << str.c_str() << endl; // 输出 字符串
}