Please help - class / templates use?

D

David Goodyear

At the moment im experimenting with ideas in C++ and would really like to
solve the following, please please help.

Sorry i dont even know what the subject is this would come under? :(
Sorry if this is elsewhere in the newsgroup & please reply with links to
threads or something? (I dont know what to look for :( )
(Terms are prob. all wrong as well :( )

The problem:
I have a data structure represented by a class. Use a date class as an
example i guess.
I have a GUI that the user can output the data to, or a file it can be
formatted and cout used to output it.
There are thousands of these date objects that need to be output, to some
form of output device.

The output device is chosen at run-time and hence the class that will be
used to format the data, from the date objects
and output it.

A class may be a GUI, File, etc...

Is there anyway I could write some base class / interface in C++, that when
classes inherit the interface class
certain methods would have to be written?. I.E. all classes must have a
function that is void Output_Short_Date?

I'm not sure where to start on this bit even? Any pointers?

The idea was to instant the appropiate class and then do function pointers
before the loop, so that one line in the code, would send
the date to the correct output.

Just to give idea - syntax is of course all out without editor to hand

switch (userChoice) {
case GUI:
//set outputDateFormatter to GUIFormatter
break;
case FILE:
//set outputDateFormatter to FileFormatter
break;
}

loop {
*outputDateFormatter (dateObject localDate);
}

would go to the correct instance of the outputter, and display the date.

The alternative is to use conditional checks but obviously these would slow
down the loops by quite a bit the higher the number of times they were
executed.

Any suggestions on what i can look up to do the above idea, (if your really
bored examples would be fantastic. My main problem is Im not sure how you
can use Templates in C++ to do this, function pointers, or what other
relevant stuff may help do this easily.

Anyway thanks for reading and any help would be great

Happy XMAS all.
 
V

Victor Bazarov

David Goodyear said:
At the moment im experimenting with ideas in C++ and would really like to
solve the following, please please help.

Sorry i dont even know what the subject is this would come under? :(

Seems like "serialisation".
Sorry if this is elsewhere in the newsgroup & please reply with links to
threads or something? (I dont know what to look for :( )

Input/Output, serialisation.
(Terms are prob. all wrong as well :( )

The problem:
I have a data structure represented by a class. Use a date class as an
example i guess.
I have a GUI that the user can output the data to, or a file it can be
formatted and cout used to output it.
There are thousands of these date objects that need to be output, to some
form of output device.

The output device is chosen at run-time and hence the class that will be
used to format the data, from the date objects
and output it.

The device should really be in control, I guess.
A class may be a GUI, File, etc...

Is there anyway I could write some base class / interface in C++, that when
classes inherit the interface class
certain methods would have to be written?.
Sure.

I.E. all classes must have a
function that is void Output_Short_Date?

In most cases, if the number of different data structures is limited
and (which is more important) stable and static, then you can add as
many member functions to that base class as there are different data
structures. If (more difficult) the data are not known and just as
dynamic as the input source or output device, you will need some kind
of inter-binding schema, like IO for only basic (fundamental) types,
something like Output_Field_Int or Output_Field_String, and then make
your data structures form their own output from those fundamental
types (fields).
I'm not sure where to start on this bit even? Any pointers?

Go to comp.programming and ask about serialisation. It's not really
language-specific. Or ask in comp.object (if OOD is what you would
like to accomplish).
The idea was to instant the appropiate class and then do function pointers
before the loop, so that one line in the code, would send
the date to the correct output.

For that the mechanism (I know of) is called "serialiser factory" or
"serialiser registry". Basically, you make every type that wants to
be input/output register its serialiser. Then when it's time to be
output, match up (somehow; say, by the name of the serialiser) the
object with the serialiser, give the serialiser the device and let
it do its job...

Victor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Help please 8
Troubles with Fullpage / please help 0
Please help 7
Help to decide which sw to use 1
Please help 2
Need help again please 19
Hello and Help please :-) 1
Code help please 4

Members online

Forum statistics

Threads
474,156
Messages
2,570,878
Members
47,405
Latest member
DavidCex

Latest Threads

Top