Object oriented programming concepts and resource management
<p>So I have a <code>FileReader</code> class, it looks like this: </p>

<pre><code>#define DISALLOW_COPY(type) \
    type(const type&amp;); \
    void operator=(const type&amp;)

class FileReader { 
    FILE *file; 
    DISALLOW_COPY(FileReader);
protected:
    unsigned char *data; 
    long size;
public:
    FileReader(const char *filename);
    ~FileReader();
    unsigned long getSize();
    unsigned char *getFileData();
};


FileReader::FileReader(const char *filename) {

    file = NULL; data = NULL;
    if (!(file = fopen(filename, "rb"))) { throw std::runtime_error("File could not be opened."); }
    fseek(file,0,SEEK_END);
    size = ftell(file);
    rewind(file);
    data = new unsigned char [size];
    VERIFY(size == (long)fread(data, 1, size, file));
    fclose(file);
#ifdef DEBUG
    PRINT("FileReader opening file "); printf("%s, %ld bytes.\n",filename,size);
#endif
}
FileReader::~FileReader() {
    delete[] data;
}
unsigned char *FileReader::getFileData() { return data; }
unsigned long FileReader::getSize() { return size; }
</code></pre>

<p>By itself it functions quite well. </p>

<p>When i create a <code>FileReader</code> I must do so with a filename that specifies what file it has to open, and when I am finished with it, it automatically cleans up after itself. </p>

<p>But I find I'm having a bit of trouble putting this class to good use. You see, if I try to use it from another class, for instance an <code>Image</code> class which represents a raster image, I'm not <strong>always</strong> creating an <code>Image</code> by reading it from a file. So, I don't want to inherit <code>FileReader</code> from <code>Image</code>. </p>

<p>Neither can I make <code>FileReader</code> a member of <code>Image</code>, because I still need to initialize (and read out a file) when I initialize my <code>Image</code>. </p>

<p>What I can do, however, is use <code>FileReader</code> in passing in <code>Image::loadFile()</code>, but then I must allocate a new buffer to store all the data, since <code>FileReader</code> will be cleaned up at the end of the function call. </p>

<p>At first I thought that RAII was a great idea, but now, I'm not so sure. It's great for handling exceptions, but I want to prevent having to move my data around like this, while keeping a clean interface that will help prevent memory management nightmares. Is there a way to do that? Seems to me like I have to significantly restructure things in order to avoid juggling all the data across a series of dynamically allocated buffers. Should I use smart pointers? </p>
