Extracting, then passing raw data into another class - How to avoid copying twice while maintaining

Posted by Kache4 on Stack Overflow See other posts from Stack Overflow or by Kache4
Published on 2010-03-25T00:18:32Z Indexed on 2010/03/25 0:23 UTC
Read the original article Hit count: 692

Filed under:
|
|

Consider a class Book with a stl container of class Page. each Page holds a screenshot, like page10.jpg in raw vector<char> form.

A Book is opened with a path to a zip, rar, or directory containing these screenshots, and uses respective methods of extracting the raw data, like ifstream inFile.read(buffer, size);, or unzReadCurrentFile(zipFile, buffer, size). It then calls the Page(const char* stream, int filesize) constructor.

Right now, it's clear that the raw data is being copied twice. Once to extract to Book's local buffer and a second time in the Page ctor to the Page::vector<char>. Is there a way to maintain encapsulation while getting rid of the middleman buffer?

© Stack Overflow or respective owner

Related posts about c++

Related posts about filestream