STL typedefs and base class pointer problem

Discussion in 'C++' started by emerth, Aug 7, 2003.

  1. emerth

    emerth Guest

    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
    emerth, Aug 7, 2003
    #1
    1. Advertising

  2. emerth

    John Dibling Guest

    On 7 Aug 2003 14:44:58 -0700, (emerth) wrote:

    >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
    John Dibling, Aug 7, 2003
    #2
    1. Advertising

  3. emerth

    root Guest


    >
    > 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?

    >
    > A pointer to the base class seems ideal for this.
    >
    root, Aug 7, 2003
    #3
  4. emerth

    emerth Guest

    > 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.


    Yes, I understand. Your example is most helpful. Thankyou very much!
    emerth, Aug 8, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tirath
    Replies:
    3
    Views:
    687
    Ivan Vecerina
    Oct 12, 2003
  2. eiji
    Replies:
    5
    Views:
    448
    Rolf Magnus
    Dec 6, 2005
  3. Alberto Luaces
    Replies:
    6
    Views:
    373
    Fraser Ross
    Jun 19, 2006
  4. asd
    Replies:
    2
    Views:
    380
  5. , India
    Replies:
    8
    Views:
    925
    gwowen
    Aug 18, 2010
Loading...

Share This Page