My current project involves reading a particular data format.
It would be convenient to be able to do something like:
class DataClass {
friend bool FileRunner::read_data (const vector<string>&);
...
};
class OtherDataClass {
friend bool FileRunner::read_data (const vector<string>&);
...
};
class FileRunner {
DataClass m_data;
OtherDataClass m_data2;
bool read_data (const vector<string>&);
...
};
However that gives the error:
error: member 'bool FileReader::read(std::string)' declared as friend
before type 'FileRunner' defined
Why I don't want to simply make FileRunner a friend class: FileRunner
is the workhorse class of the program; I want to make sure that all
post-initialization access goes through the appropriate accessors, and
I want to be sure that certain parts aren't changed after
initialization.
Why I don't split off the reading parts into a separate class and make
that whole class a friend: The reading parts need access to some of
the running functions.
Why I don't do all the initialization in the DataClass's constructor:
The initialization process needs to be able to call other functions in
FileRunner, and isn't isolatable to a single point.
I'm planning on rewriting it to be a single external function (friend
to all needed classes) with an added FileRunner& argument, but because
it references fields within FileRunner heavily, that would be very
tedious. Any alternatives?
(I did not design the file format.)