Is there a vector that can store any type?

Discussion in 'C++' started by Erik2000, Aug 25, 2003.

  1. Erik2000

    Erik2000 Guest

    I would really like to use a vector that can store any type: a float,
    double, int, or object. Is there a way to do this?


    --
    Posted via http://dbforums.com
    Erik2000, Aug 25, 2003
    #1
    1. Advertising

  2. "Erik2000" <> wrote in message
    news:...
    >
    > I would really like to use a vector that can store any type: a float,
    > double, int, or object. Is there a way to do this?
    >


    Not without a lot of work. So much work that some would say that you have a
    design problem not a programming problem, and you should really reconsider
    why you need a vector of any type.

    Nevertheless, some kind soul has already done the work for you, check out
    the 'any' type at boost.

    http://www.boost.org/doc/html/any.html

    john
    John Harrison, Aug 25, 2003
    #2
    1. Advertising

  3. Erik2000

    Erik2000 Guest

    Hi John!



    The 'any' type looks like what I am looking for. I will check it out.



    Thanks very much for the tip!



    Erik


    --
    Posted via http://dbforums.com
    Erik2000, Aug 25, 2003
    #3
  4. Erik2000

    Anand Guest

    I tried something using list this week. I hope same thing can be done
    with vector too.

    class myBaseData
    {
    virtual void myMethod1() = 0;
    };

    class myIntData : public myBaseData
    {
    public: myMethod1() { }
    };

    class myFloatData: public myBaseData
    {
    public: myMethod1() { }

    };

    typedef list<myBaseData *> myBaseDataList;

    main()
    {
    myBaseDataList myDataTypeList;

    for(myBaseDataList::iterator ii = myDataTypeList.begin();
    ii!=myDataTypeList.end();ii++)
    {
    myBaseData *tmp = *ii;
    tmp->myMethod1();
    }
    }


    HTH
    Erik2000 <> wrote in message news:<>...
    > Hi John!
    >
    >
    >
    > The 'any' type looks like what I am looking for. I will check it out.
    >
    >
    >
    > Thanks very much for the tip!
    >
    >
    >
    > Erik
    Anand, Aug 26, 2003
    #4
  5. Anand wrote:

    Please don't top-post. Re-read section 5 of the FAQ for posting guide lines:

    http://www.parashift.com/c -faq-lite/

    > I tried something using list this week. I hope same thing can be done
    > with vector too.
    >
    > class myBaseData
    > {
    > virtual void myMethod1() = 0;
    > };
    >
    > class myIntData : public myBaseData
    > {
    > public: myMethod1() { }


    Error: No return type.

    > };
    >
    > class myFloatData: public myBaseData
    > {
    > public: myMethod1() { }


    Error: No return type.

    >
    > };
    >
    > typedef list<myBaseData *> myBaseDataList;
    >
    > main()


    Error: No return type.

    > {
    > myBaseDataList myDataTypeList;
    >
    > for(myBaseDataList::iterator ii = myDataTypeList.begin();
    > ii!=myDataTypeList.end();ii++)


    You should use ++ii here. Prefer pre-increment (or decrement) to
    post-increment (or decrement) when you have a choice.

    > {
    > myBaseData *tmp = *ii;


    Why the extra variable?

    (*ii)->myMethod1();

    > tmp->myMethod1();
    > }
    > }
    >


    This is a pretty basic example of collecting different types under a
    common base, but its usefulness is questionable. You haven't provided
    any way of retrieving or setting the value of the variables, and that's
    the only part that's very difficult. It's also not terribly convenient,
    since each new type requires you do define a new class.

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Aug 27, 2003
    #5
  6. Erik2000

    Greg Schmidt Guest

    On Wed, 27 Aug 2003 06:44:26 GMT, "SenderX" <> wrote:

    >> You should use ++ii here. Prefer pre-increment (or decrement) to
    >> post-increment (or decrement) when you have a choice.

    >
    >Its ++C, not C++ damn it.
    >
    >;)


    No, Bjarne was shooting for maximum backwards compatibility with C, so
    he felt that since C++ returns the same value as C that would be a
    better choice of name. And, judging from C99, one of the results of C++
    was that C was modified, so the decision was, in retrospect, truly
    inspired!

    --
    Greg Schmidt ()
    Trawna Publications (http://www.trawna.com/)
    Greg Schmidt, Aug 27, 2003
    #6
  7. Greg Schmidt wrote:

    > On Wed, 27 Aug 2003 06:44:26 GMT, "SenderX" <> wrote:
    >
    >
    >>>You should use ++ii here. Prefer pre-increment (or decrement) to
    >>>post-increment (or decrement) when you have a choice.

    >>
    >>Its ++C, not C++ damn it.
    >>
    >>;)

    >
    >
    > No, Bjarne was shooting for maximum backwards compatibility with C, so
    > he felt that since C++ returns the same value as C that would be a
    > better choice of name. And, judging from C99, one of the results of C++
    > was that C was modified, so the decision was, in retrospect, truly
    > inspired!
    >


    C was modified by C++ well before C99. A few things C borrowed from C++:

    * function prototypes
    * void pointers
    * the 'const' keyword

    And, while I understand you were joking, Bjarne didn't come up with the
    name 'C++'. He was calling it 'C with Classes' up until Rick Mascitti
    suggested that name (and aren't we all glad he didn't keep the original
    name?).

    -Kevin
    --
    My email address is valid, but changes periodically.
    To contact me please use the address from a recent posting.
    Kevin Goodsell, Aug 27, 2003
    #7
    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. pmatos
    Replies:
    6
    Views:
    23,772
  2. Replies:
    8
    Views:
    1,912
    Csaba
    Feb 18, 2006
  3. Javier
    Replies:
    2
    Views:
    558
    James Kanze
    Sep 4, 2007
  4. ssylee
    Replies:
    1
    Views:
    1,202
    Zachary Turner
    Jan 13, 2009
  5. Rushikesh Joshi
    Replies:
    0
    Views:
    355
    Rushikesh Joshi
    Jul 10, 2004
Loading...

Share This Page