In terms of code changes based on what you already have, the simplest is probably to provide a Page set seter that accepts a non-constant vector link or pointer, and swapwith the vector contained on the page. The call will contain an empty vector, but since the problem is excessive copying, the caller presumably does not want to save the data:
void Book::addPage(ifstream file, streampos size) {
std::vector<char> vec(size);
file.read(&vec[0], size);
pages.push_back(Page());
pages.back().setContent(vec);
}
class Page {
std::vector<char> content;
public:
Page() : content(0) {}
void setContent(std::vector<char> &newcontent) {
content.swap(newcontent);
}
};
Some people (like the Google C ++ style guide) want the reference parameters to be const, and would like you to pass the parameter newcontentas a pointer to emphasize that it is not const:
void setContent(std::vector<char> *newcontent) {
content.swap(*newcontent);
}
swap runs fast - you expect it to simply replace the pointers to the buffer and the sizes of two vector objects.
, : zip . , , , Page , . , , , . , , , , : , . , setContent / zip , .
, zip , , . , , , , , , /zip/rar , .
, " ". pages a std::vector<boost::shared_ptr<Page> >, :
void Book::addPage(ifstream file, streampos size) {
boost::shared_ptr<Page> page(new Page(file, size));
pages.push_back(page);
}
A shared_ptr ( node, refcount), . TR1, , Boost.