How can I implement a plolimorfic queue in a type-safe manner ?

T

Timothy Madden

I want to stoare in a queue values of type LPCTSTR, int, long, BYTE, WORD,
DWORD and many enum types.

Can I write such a queue in a type safe maner (idealy no casts involved) ?

Thank you
"Timothy Madden"
Romania
 
H

Howard

Timothy Madden said:
I want to stoare in a queue values of type LPCTSTR, int, long, BYTE, WORD,
DWORD and many enum types.

Can I write such a queue in a type safe maner (idealy no casts involved) ?

Thank you
"Timothy Madden"
Romania


You can if you make a container class to hold those types of values.

One way to do it might be to create one class that has a union of variables
of each of those types. Look up the "union" keyword.

Another might be a class that has a member variable of each of those types
(not in a union), and perhaps another member that tells the user of the
class whether any given member currently holds valid data. (Alternatively,
you could just have default values for those members which your system can
deal with, such as 0.) This would allow you to actually store more than
just one data item in a single container object (but you probably don't need
that).

Another possibility would be to have a hierarchy of classes, each of which
handles a different type of internal data, all deriving from a common base
class, and then have your queue store pointers to that base class type (each
of which is new'ed with an instance of the specific sub-class you need to
use).

The problem with that last idea is finding a way to get the type of data you
want via a base-class-pointer. I think one of my first two ideas might be
simpler to implement.

-Howard
 

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

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top