我有一个类的成员函数,定义如下
int x(std::string &a, std::string &b) {
char *ptr = another_member.getStringMember().c_str() //I am storing the pointer
cout << ptr << endl;
a="hello";
cout << ptr << endl;
}
输出是
字符串成员
Hello
你能解释一下为什么会发生这种情况吗?谢谢
最有可能是因为another_member.getStringMember
and a
是同一个字符串。
在这种情况下,使用实际上并不合法ptr
修改字符串后a="hello";
因为变异操作会使之前获得的指针无效。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)