我想通過std :: string :: operator >>在C++中分配std :: string * ptr。在C++中通過std :: istream分配字符串對象
我下面有一堂課。
class A{
public:
A(){
ptr = new std::string();
}
A(std::string& file){
ptr = new std::string();
std::ifstream ifs(file);
std::stringstream ss;
ss << ifs.rdbuf();
*ptr=ss.str();
ifs.close();
}
~A(){
delete ptr;
}
void output(std::ostream& stream) const{
stream << *ptr;
}
void input(std::istream& stream) const{
stream >> *ptr;
}
private:
std::string *ptr;
};
int main(void){
std::string file="./file";
std::string dfile="./dump";
std::ofstream ofs(file);
A a(file);
a.output(ofs);
std::ifstream ifs(dfile);
A b();
b.input(ifs);
return 0;
}
假設 「./file」 包含以下內容:
The first form (1) returns a string object with a copy of the current contents of the stream.
The second form (2) sets str as the contents of the stream, discarding any previous contents.
我證實的 「./dump」 的內容是一樣的 「./file」。 但是,字符串對象,我從b.input得到(B的* PTR)(「./轉儲」)只是一小串delimitered的空間,這就是
The
我怎樣才能獲得整個文本? 謝謝
危險! 'A'違反了三條規則。 – aschepler