By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,332 Members | 1,505 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,332 IT Pros & Developers. It's quick & easy.

Please help - class / templates use?

P: n/a
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.
---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.552 / Virus Database: 344 - Release Date: 15/12/2003
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"David Goodyear" <da**@kent.ac.uk> wrote...
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
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.