最近我有c_str()的问题.下面是示例代码片段
#include<bits/stdc++.h>
#include<unistd.h>
using namespace std;
class har{
public:
string h;
har(string str){
h=str;
}
};
int main(){
har *hg=new har("harish");
const char *ptr=hg->h.c_str();
delete hg;
cout<<ptr<<endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我得到输出"harish"....我已经销毁了对象,但我仍然得到输出..是c_str()再次在堆中分配内存.
c++ ×1