Image loader/writer design (no API, pure C++ on Windows) I have an image loader, now only for bitmaps. I'm a little confused because I want to split my code to different classes, one for writing a BMP data to a file (WRITER), one for loading BMP data from file, or from pre-generated dataset (1) and storing it in its private members.( (1) like a noise function saved to an array).

Currently I have the 2 class for this. The header:

namespace PND
{

    inline void out(const ::std::string); //standard output log
    inline void err(const ::std::string); //error log

    bool file_exists(::std::string m_filename) //this could be used in both classes
    {
        ::std::ifstream infile(m_filename+".bmp");
        return infile.good();
    }

    class BMP
    {
    public:
        BMP();
        BMP(const BMP&amp;);//copy csrt
        ~BMP();
        BMP&amp; operator= (BMP other); //ass. operator

        bool loadData(::std::string filename);//from file, filename is the file's filename
        bool loadData(::std::string filename,const int width, const int height, const unsigned char* data, const unsigned int length);//from array, filename is user defined
        //they are simple pixel data accessors with out of bounds handling
        unsigned char getData_B(const unsigned int x, const unsigned int y);
        unsigned char getData_G(const unsigned int x, const unsigned int y);
        unsigned char getData_R(const unsigned int x, const unsigned int y);

        unsigned int* getSize() { return m_size; }; //some getter
        ::std::string getName() { return m_filename; };

    private:

        void swap(BMP&amp; other);
        void freeData();
            //the bitmap data:
        BITMAPINFOHEADER m_infoheader;
        BITMAPFILEHEADER m_fileheader;
        ::std::string m_filename;
        unsigned char* m_data;
        unsigned int m_size[2];
        long __m_size; //it is the length of the m_data array (num of pixels * 3)
    };

    class WRITER
    {
    public:
        bool writeData(::std::string filename, const int width, const int height, const unsigned char* data, const unsigned int length);
        bool writeData(::std::string filename, const BMP&amp; bmp);//now this is the problem, more info below the code

    private:
        ::std::ofstream outstream;

        bool openFile(::std::string filename);
        bool writeHeader(const int width,const int height);
        bool writeFile(const unsigned char* data, const unsigned int length);
        bool closeStream();
    };
}


I'm in trouble because I would like to access (read only) the bitmap data (headers, pixel data) from the image - that was saved to a BMP object - in the WRITER class. But because they are private members I can not access them in the writeheader() and writefile() functions. It would be nice to access them without making them public members, and I also try to avoid using inheritance in this case since a WRITER is not really a BMP. Lots of gettersare also not the best way for accessing arrays I think.

So the BMP could only load and save the image data. And with a WRITER I can write this data to a .bmp file. I want to make a pretty clean interface with public functions, and do not mix classes if not neccessary.
The only standard ways to access private data from another class are:


Getters  
Friend class


If you want to make a friend class, just do something like this:

class Writer ;

class BMP
{
    //...
private:
friend class Writer ;
}


Now the Writer class can access all of BMP's data fields.  

I would personally suggest using getters though. You could have getters for the header information and use operator [] for accessing any arrays.

unsigned char&amp; BMP::operator [] (const size_t i) {
    return m_data [i] ;
}

const unsigned char&amp; BMP::operator [] (const size_t i) const {
    return m_data [i] ;
}