我还在学习c ++,所以请耐心等待.我正在编写一个关于boost文件系统路径的简单包装器 - 我在返回临时字符串方面遇到了奇怪的问题.这是我的简单类(这不是确切的,但非常接近):
typedef const char* CString;
typedef std::string String;
typedef boost::filesystem::path Path;
class FileReference {
public:
FileReference(const char* path) : mPath(path) {};
// returns a path
String path() const {
return mPath.string();
};
// returns a path a c string
CString c_str() const {
return mPath.string().c_str();
};
private:
Path mPath;
}
Run Code Online (Sandbox Code Playgroud)
使用下面的小测试代码:
FileReference file("c:\\test.txt");
OutputDebugString(file.path().c_str()); // returns correctly c:\test.txt
OutputDebugString(file.c_str()); // returns junk (ie îþîþîþîþîþîþîþîþîþîþî.....)
Run Code Online (Sandbox Code Playgroud)
我很确定这必须处理临时工,但我无法弄清楚为什么会这样 - 不应该一切都正确复制?
CString c_str() const {
return mPath.string().c_str();
};
Run Code Online (Sandbox Code Playgroud)
mPath.string()返回一份std::string.该副本存储在临时表中,该表达式将在此表达式的末尾销毁.
.c_str() 返回一个指向内存的指针,当字符串被销毁时,即在该表达式结束时将被销毁.
您正在返回一个已经销毁的内存指针.
看起来像mPath.string()按值返回一个字符串。一旦 FileReference::c_str() 返回,该临时字符串对象就会被破坏,因此它的 c_str() 变得无效。c_str()使用这样的模型,如果不为字符串引入某种类或静态级别变量,就不可能创建函数。
考虑以下替代方案:
//Returns a string by value (not a pointer!)
//Don't call it c_str() - that'd be misleading
String str() const
{
return mPath.string();
}
Run Code Online (Sandbox Code Playgroud)
或者
void str(String &s) const
{
s = mPath.string();
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1105 次 |
| 最近记录: |