I'm still learning c++, so please bear with me. I'm writing a simple wrapper around boost filesystem paths -- I'm having strange issues with returning temporary strings. Here's my simple class (this is not exact, but pretty close):
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;
}
With the little test code below:
FileReference file("c:\\test.txt");
OutputDebugString(file.path().c_str()); // returns correctly c:\test.txt
OutputDebugString(file.c_str()); // returns junk (ie îþîþîþîþîþîþîþîþîþîþî.....)
I'm pretty sure this has to deal with temporaries, but I can't figure out why that would be -- shouldn't everything be copying correctly?