STL typedefs and base class pointer problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • emerth

    STL typedefs and base class pointer problem

    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
  • root

    #2
    Re: STL typedefs and base class pointer problem

    [color=blue]
    >
    > 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.[/color]

    Just curious, how do u plan to use this ptr in your code?
    [color=blue]
    >
    > A pointer to the base class seems ideal for this.
    >[/color]

    Comment

    • John Dibling

      #3
      Re: STL typedefs and base class pointer problem

      On 7 Aug 2003 14:44:58 -0700, emerth@hotmail. com (emerth) wrote:
      [color=blue]
      >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;[/color]

      The keys should probably be something like std::strting, or you'll
      BURG.
      [color=blue]
      >A pointer to the base class seems ideal for this.[/color]

      You are wanting to use STL polymorphically . You can't; STL isn't
      object-oriented.
      [color=blue]
      >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? ;-)[/color]

      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<typena me 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

      Comment

      • emerth

        #4
        Re: STL typedefs and base class pointer problem

        > template<typena me T>[color=blue]
        > 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.[/color]

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

        Comment

        Working...