How do I go about using the constructor for a member which is a string? Here is an example (which is wrong I realize)
class Filestring {
public:
string sFile;
Filestring(const string &path)
{
ifstream filestream(path.c_str());
// How can I use the constructor for the member sFile??
// I know this is wrong, but this illustrates what I want to do.
string sFile((istreambuf_iterator<char>(filestream)), istreambuf_iterator<char>());
}
};
So basically I want to be able to use the member sFile’s constructor without doing a string copy. Is there a way to achieve this through assignment?
The best you can use is
string::assign:But in C++11 there is a move assignment operator for string so doing the following is almost as efficient (there is no copy of character data, the character data are moved):
Yet another trick when C++11 move assignment is not avaliable is to use
std::swaporstring::swap. The efficiency would be probably almost identical to the move assignment variant.