473,324 Members | 2,178 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,324 software developers and data experts.

Please help - class / templates use?

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

Similar topics

21
by: Sebastian Faust | last post by:
Hi, is a construction like the following possible: template<class view_model> class template_clase { protected: template_clase() {} virtual ~template_clase() {}
4
by: Sebastian Faust | last post by:
Hi, I have 4 questions related to templates. I wanna do something like the following: template<typename T> class Template { public: Template_Test<T>()
4
by: Gert Van den Eynde | last post by:
Hi all, A beginners question.... I've got a template class template <class T> classA {...} In an other class, I want to pass a pointer to an instance of classA as a function argument....
4
by: KK | last post by:
Hello all, I have class 'atr' which is based on templates. My idea was to initialize it two scenarios 1. std::string case 2. other data types I have defined the Init for the above different...
6
by: wkaras | last post by:
I tried a couple of compilers, and both gave errors compiling this: template <bool fin, typename T> T foo(T val); template <typename T> T foo<true, T>(T val) { return(val); } But both gave...
4
by: Joseph Turian | last post by:
Hi, What is the correct syntax to get the bar<T>::f<int, unsigned>() function to compile in the following fragment? Thanks, Joseph class foo {
8
by: Rahul | last post by:
Hi, Is there a way to partially specialize only a member function of a template class (not the whole class). e.g. template <typename A, typename B> class Base { public:
2
by: thuythu | last post by:
Please help me.... I used and Javascript to view the data. But when i click button open a popup windows, then select data and click save button. The popup close and return the main page, but the...
15
by: akomiakov | last post by:
Is there a technical reason why one can't initialize a cost static non- integral data member in a class?
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
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...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
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.