Different types of object in a single list?

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

    Different types of object in a single list?

    Is it possible to put different types of objects in a single STL list? If
    not what STL template should be used which will contains various objects of
    different types?

    Thanks for your help!


  • Victor Bazarov

    #2
    Re: Different types of object in a single list?

    "alg" <alg36897@yahoo .com> wrote...[color=blue]
    > Is it possible to put different types of objects in a single STL list? If
    > not what STL template should be used which will contains various objects[/color]
    of[color=blue]
    > different types?[/color]

    Please search the archives for "heterogene ous container".
    The main question you should ask yourself is "why do I want
    to have them in one container?"

    Victor


    Comment

    • Ioannis Vranos

      #3
      Re: Different types of object in a single list?

      "alg" <alg36897@yahoo .com> wrote in message
      news:gI0Ra.5627 0$0v4.3832540@b gtnsc04-news.ops.worldn et.att.net...[color=blue]
      > Is it possible to put different types of objects in a single STL list? If
      > not what STL template should be used which will contains various objects[/color]
      of[color=blue]
      > different types?[/color]


      You can make the container store void *s.







      --
      Ioannis

      * Programming pages: http://www.noicys.freeurl.com
      * Alternative URL 1: http://run.to/noicys
      * Alternative URL 2: http://www.noicys.cjb.net

      Comment

      • Yamin

        #4
        Re: Different types of object in a single list?


        "alg" <alg36897@yahoo .com> wrote in message
        news:gI0Ra.5627 0$0v4.3832540@b gtnsc04-news.ops.worldn et.att.net...[color=blue]
        > Is it possible to put different types of objects in a single STL list? If
        > not what STL template should be used which will contains various objects[/color]
        of[color=blue]
        > different types?
        >
        > Thanks for your help!
        >
        >[/color]
        This question does get asked often enough, and I always ask myself why? Why
        do you need to store objects of different types and how do you plan to work
        with them?

        Suppose we do have a magical container that can store objects of different
        types.
        *************** *************** *************** ******
        A a;
        B b;
        C c;

        MagicContainer list;
        list.insert(a);
        list.insert(b);
        list.insert(c);
        *************** *************** *************** ******
        all fair so far. Now how do we get stuff out of the list?

        A *a = list.front(); ?
        How do we know the element is of type A? Maybe its of type B, or C?
        You see the issue?

        So now you can either create the list as a (void*) list and magically know
        what to cast the elements back to.
        You could create some kind of container structure that includes the type.

        struct
        {
        int myType;
        void *data
        }

        You could create a union with an explicit type

        struct
        {
        int myType;
        union
        {
        A a;
        B b;
        C c;
        }
        }

        You could do it the C++ way and create a common base class, make the list of
        that type. Make all common operations virtual and away you go, its a
        beautiful thing. If you want to get messy a bit and mix C/C++, include a
        myType variable as part of the base class, and have each derived class set
        that variable to its own thing. Then if you really need to, you know what
        to cast it to using dynamic_cast.

        Yamin


        Comment

        • Raoul Gough

          #5
          Re: Different types of object in a single list?

          "Sergey Tursanov" <_gsr_@hippo.ru > writes:
          [color=blue]
          > "Yamin" <absdfsd@sdfdas fsd.com> wrote in message
          >[color=green]
          >> You could create a union with an explicit type
          >>
          >> struct
          >> {
          >> int myType;
          >> union
          >> {
          >> A a;
          >> B b;
          >> C c;
          >> }
          >> }
          >>[/color]
          >
          > It seems to me that you cannot place classes with constructors in union.
          > Just because compiler don't know which constructor to call when you
          > initialize union.[/color]

          You're absolutely correct, and the standard explicitly says that
          classes with non-trivial constructors are not allowed as union
          members. Quoting from section 9.5:

          An object of a class with a non-trivial constructor (12.1), a
          non-trivial copy constructor (12.8), a non-trivial destructor
          (12.4), or a non-trivial copy assignment operator (13.5.3, 12.8)
          cannot be a member of a union, nor can an array of such objects.

          --
          Raoul Gough
          "Let there be one measure for wine throughout our kingdom, and one
          measure for ale, and one measure for corn" - Magna Carta

          Comment

          • puppet_sock@hotmail.com

            #6
            Re: Different types of object in a single list?

            "Victor Bazarov" <v.Abazarov@att Abi.com> wrote in message news:<EV0Ra.730 78$Ph3.6922@scc rnsc04>...[color=blue]
            > "alg" <alg36897@yahoo .com> wrote...[color=green]
            > > Is it possible to put different types of objects in a single STL list? If
            > > not what STL template should be used which will contains various objects[/color]
            > of[color=green]
            > > different types?[/color]
            >
            > Please search the archives for "heterogene ous container".
            > The main question you should ask yourself is "why do I want
            > to have them in one container?"[/color]

            Probably because the OP has learned to code in "another language"
            where there are things *called* objects that are really variants.

            It is possible to code up something that does much the same job
            as a variant, and even make it fit properly in a standard container.
            I'm not real sure if it's ever the best choice. Maybe if you were
            writing something like a postscript interpreter where you needed
            to have a stack that held all different kinds of objects from
            characters, to procedures, to fonts, to dictionaries. Or maybe not.
            Socks

            Comment

            • Samuele Armondi

              #7
              Re: Different types of object in a single list?

              "alg" <alg36897@yahoo .com> wrote in message
              news:gI0Ra.5627 0$0v4.3832540@b gtnsc04-news.ops.worldn et.att.net...[color=blue]
              > Is it possible to put different types of objects in a single STL list? If
              > not what STL template should be used which will contains various objects[/color]
              of[color=blue]
              > different types?
              >
              > Thanks for your help!
              >
              >[/color]

              The only time I needed to do this was when writing a console for a game, and
              even then I was only dealing with POD's, which I stored in a union. But you
              need to make sure you know you know for sure that you are getting/setting
              the correct union members (by using some sort of flag) otherwise you will
              see fireworks (seen plenty of those when debugging the console!)
              S. Armondi


              Comment

              Working...