Sto ancora imparando C++, quindi per favore portami con me. Sto scrivendo un semplice wrapper per incrementare i percorsi del filesystem - Sto avendo strani problemi con il ritorno di stringhe temporanee. Ecco il mio semplice classe (questo non è esatto, ma abbastanza vicino):Std temporaneo: stringhe che restituiscono spazzatura
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;
}
Con il codice di prova poco sotto:
FileReference file("c:\\test.txt");
OutputDebugString(file.path().c_str()); // returns correctly c:\test.txt
OutputDebugString(file.c_str()); // returns junk (ie îþîþîþîþîþîþîþîþîþîþî.....)
Sono abbastanza sicuro che questo ha a che fare con temporanei, ma io non riesco a capire perché sarebbe - non dovrebbe essere tutto copiato correttamente?
qual è la fonte di 'OutputDebugString()'? –
Cringato a 'typedef''ing proprio' CString' ... – Blindy
@OttoAllmendinger - È una [API Windows] (http://msdn.microsoft.com/en-us/library/windows/desktop/aa363362 (v = vs.85) .aspx). –