STL typedefs and base class pointer problem

E

emerth

Suppose I have these 4 typedefs, where each of the 2nd parameter in
each is a simple class that just contains a different number of strings:

typedef map < char*, cctrlrec_data, ltstr > ctrlrec_table2;
typedef map < char*, ctyperec_data, ltstr > typerec_table2;
typedef map < char*, chotrec_data, ltstr > hotrec_table2;
typedef map < char*, cmtxtrec_data, ltstr > mtxtrec_table2;

I have a stream of XML data coming across a socket and I want to parse
it out into instances of cctrlrec_data, ctyperec_data, chotrec_data, or
cmtxtrec_data, which then will be stuffed into the appropriate typedef'd
map.

My parser will tell me which kind of object to instantiate, and
it produces the data for a number of each in series but does mix
them up in series.

eg: parser output is:
-- start parsing
cctrlrec_data strings
..
..
..
cctrlrec_data strings
ctyperec_data strings
..
..
..
ctyperec_data strings
chotrec_data strings
..
..
..
chotrec_data strings
cmtxtrec_data strings
..
..
..
cmtxtrec_data strings
-- done parsing

I'd like to have a pointer that I could just change to point at the
appropriate map when the parser code starts producing a different
type of output object.

A pointer to the base class seems ideal for this.

But how does one declare a pointer to the base class when that is a
template class? Can one do this? Am I completely out to lunch, & should
consider some other method of doing this? ;-)

TIA!
Eric
 
J

John Dibling

Suppose I have these 4 typedefs, where each of the 2nd parameter in
each is a simple class that just contains a different number of strings:

typedef map < char*, cctrlrec_data, ltstr > ctrlrec_table2;
typedef map < char*, ctyperec_data, ltstr > typerec_table2;
typedef map < char*, chotrec_data, ltstr > hotrec_table2;
typedef map < char*, cmtxtrec_data, ltstr > mtxtrec_table2;

The keys should probably be something like std::strting, or you'll
BURG.
A pointer to the base class seems ideal for this.

You are wanting to use STL polymorphically. You can't; STL isn't
object-oriented.
But how does one declare a pointer to the base class when that is a
template class? Can one do this? Am I completely out to lunch, & should
consider some other method of doing this? ;-)

Consider a new method. Here's the thing: ctrlrec_table2 (etc) has no
base class; at least not one defined by the standard. (eg, if your
implementation of std::map is derived from something, that's
implementation-specific) Templated types aren't derived from base
classes. Each concrete instance of a template is it's own, seperate
thing.

Here's an example:

<code>
template<typename T>
class templ
{
void f();
};

typedef templ<float> my_float_templ;
</code>

my_float_templ doesn't have a base class. It's not derived from
anything.

</dib>
John Dibling
Witty banter omitted for your protection
 
R

root

I'd like to have a pointer that I could just change to point at the
appropriate map when the parser code starts producing a different
type of output object.

Just curious, how do u plan to use this ptr in your code?
 
E

emerth

template said:
class templ
{
void f();
};

typedef templ<float> my_float_templ;
</code>

my_float_templ doesn't have a base class. It's not derived from
anything.

Yes, I understand. Your example is most helpful. Thankyou very much!
 

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

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top