2

I've got this piece of code in C++ from someone else that I am now working with but I'm not sure why the "std::string()" is been added.

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);

My question is, since str() returns a string, and c_str() gets a string and transforms it into c string, why do we need to put it inside the "string()"? Could it not be writen like:

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 回答