Re: pure virtual function with unspecified parameters?
* Victor Bazarov:
Markus Dehmann wrote:
I have an abstract base class called Data. It has a pure virtual
function
virtual void write(std::ostream& out) =0;
which writes the internal data to a stream. Now the problem is that
this is not appropriate for some implementations of the class. Some
implementations have an internal representation that they should
rather write into several separate files. So for those, something like
this would be more appropriate:
void write(const std::string& outputDirectoryName);
What would be a good design for the abstract base class in this case?
struct OutputDestBase { virtual ~OutputDestBase() {} };
void write(OutputDestBase const& where) = 0;
Now, make every class have its own corresponding member class that will
derive from 'OutputDestBase' and have the contents known to the owning
class alone, which will permit static_cast (or dynamic_cast if you prefer
to have error-checking) to that derived member class before extracting
any necessary information from it.
Ouch.
Necessity of casting, except for interfacing to low level C style code,
is generally symptomatic of bad design.
[snip]
You're close. Keep digging in the same direction.
Separation of concerns is a good design guideline, but the current
direction is opposite.
Cheers, & hth.,
- Alf
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?