Re: Strange friend ordering
In article <1172256788.737649.139770@h3g2000cwc.googlegroups.com>,
ralphmerridew@gmail.com says...
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>&);
...
};
You haven't said enough to say whether it's a good idea, but what you
have right now is _almost_ equivalent to something like this:
class base {
protected:
virtual bool read_data(const vector<string> &)=0;
};
class DataClass : virtual base {
// code that uses read_data
};
class OtherDataClass : virtual base {
// code that uses read_data
};
class FileRunner : DataClass, OtherDataClass {
protected:
bool read_data(const vector<string> &s) {
// whatever
}
};
Since read_data is declared in base, it's usable from both DataClass and
OtherDataClass (except in their ctors). Since FileRunner inherits from
DataClass and OtherDataClass, it implicitly contains an embedded object
of each type and has access to the public/protected functions of both.
OTOH, this is an example of the "deadly diamond" inheritance pattern,
which isn't particularly popular.
OTOH, you've implied that you want to use read_data from the ctor of
DataClass and/or OtherDataClass. That's not possible with code like
above -- to allow that, you'd have to move the definition of read_data
to base, instead of just declaring it there.
Side note: the inheritance above is (intentionally) _private_, so the
usual concerns about inheritance such as LSP don't apply.
--
Later,
Jerry.
The universe is a figment of its own imagination.