2

現在一緒に作業している他の誰かから C++ でこのコードを入手しましたが、「std::string()」が追加された理由がわかりません。

std::ifstream File;
std::stringstream FileName;
FileName << Name; //Name being a string that has been passed as an input to the function.
                 // Eg."MyFile"
newFileName << ".txt"; //"MyFile.txt"

File.open(std::string(FileName.str()).c_str(), std::ios::in | std::ios::binary);

私の質問は、str() は文字列を返し、c_str() は文字列を取得して C 文字列に変換するので、なぜ「string()」の中に入れる必要があるのでしょうか? 次のように書くことはできませんか?

File.open((FileName.str()).c_str(), std::ios::in | std::ios::binary);
4

1 に答える 1

1

はい、このように書けます。

使用する

std::string(FileName.str())

は絶対に無意味です。

于 2012-11-01T15:35:47.827 に答える