468,469 Members | 1,971 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,469 developers. It's quick & easy.

pure virtual function with unspecified parameters?

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?
It seems like it should require some kind of write function, but with
flexible parameter lists for different implementations.

Certainly we don't want to deal with void pointers:

write(void* toWhatever) = 0;

that the implementation classes would cast and use however they like
because void pointers are evil.

A template also doesn't work:

template<class T>
virtual void write(T& out) = 0;

because you can't template a virtual function. And it would seem like
overkill to template the whole class on this output parameter; after
all it's just a little write function within a much larger class.

What would be a good design in this case?

Thanks!
Markus

Nov 27 '07 #1
2 3387
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.
It seems like it should require some kind of write function, but with
flexible parameter lists for different implementations.

Certainly we don't want to deal with void pointers:

write(void* toWhatever) = 0;

that the implementation classes would cast and use however they like
because void pointers are evil.

A template also doesn't work:

template<class T>
virtual void write(T& out) = 0;

because you can't template a virtual function. And it would seem like
overkill to template the whole class on this output parameter; after
all it's just a little write function within a much larger class.

What would be a good design in this case?
You're close. Keep digging in the same direction.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 27 '07 #2
On Nov 27, 10:55 am, Markus Dehmann <markus.dehm...@gmail.comwrote:
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?
It seems like it should require some kind of write function, but with
flexible parameter lists for different implementations.

Certainly we don't want to deal with void pointers:

write(void* toWhatever) = 0;

that the implementation classes would cast and use however they like
because void pointers are evil.

A template also doesn't work:

template<class T>
virtual void write(T& out) = 0;

because you can't template a virtual function. And it would seem like
overkill to template the whole class on this output parameter; after
all it's just a little write function within a much larger class.

What would be a good design in this case?
Thanks for all your answers and the lateness of this reply.

After reading up on Design Patterns, it seems to me that the Visitor
pattern would fit perfectly here and solve my problem. You all were
concerned about separation of responsibilities. So, just adding a

void accept(const Visitor& v){
v.visit(this);
}

should be fine and keep the class small. Then, different
implementations can define their own WriteVisitor version, like this:

class WriteVisitor : public Visitor {
void visit(const MyData& d){
// write by calling the appropriate functions specific to MyData
}
};

Markus

P.S.: Before getting the Visitor idea, I "solved" the problem by just
adding a

void write(const std::string& name);

function. Some implementations can write to a stream (e.g. to cout if
name=="-"), others can write to a directory of that name, etc.
Dec 7 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

11 posts views Thread by santosh | last post: by
37 posts views Thread by WittyGuy | last post: by
6 posts views Thread by pakis | last post: by
3 posts views Thread by sudhir | last post: by
21 posts views Thread by sks | last post: by
7 posts views Thread by sam_cit | last post: by
10 posts views Thread by Rahul | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by kmladenovski | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.