473,322 Members | 1,501 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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 3672
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: santosh | last post by:
Hello, I was going through the Marshal Cline's C++ FAQ-Lite. I have a doubt regarding section 33.10. Here he is declaring a pure virtual destructor in the base class. And again defining...
37
by: WittyGuy | last post by:
Hi, I wonder the necessity of constructor and destructor in a Abstract Class? Is it really needed? ? Wg http://www.gotw.ca/resources/clcm.htm for info about ]
6
by: pakis | last post by:
I am having a problem of pure virtual function call in my project. Can anyone explaine me the causes of pure virtual function calls other than calling a virtual function in base class? Thanks
3
by: sudhir | last post by:
I defined a pure virtual function like virtual void sum()=0; <--- pure virtual function but If I defined a virtual function in a base class in case of multilevel inheritance for the base...
21
by: sks | last post by:
Hi , could anyone explain me why definition to a pure virtual function is allowed ?
10
by: John Goche | last post by:
Hello, page 202 of Symbian OS Explained by Jo Stichbury states "All virtual functions, public, protected or private, should be exported" then page 203 states "In the rare cases where a...
7
by: sam_cit | last post by:
Hi Everyone, I wanted to know as to what is the exact difference between a virtual function and a pure virtual function? Thanks in advance!!!
10
by: Rahul | last post by:
Hi, I tried to create a abstract class by having a non-virtual member function as pure, but i got a compilation error saying "only virtual member functions can be pure"... I'm trying to think...
14
by: Jack | last post by:
Hi, I meet a question with it , I did not get clear the different betteen them, for example: #include <iostream>
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.