Is there a vector that can store any type?

E

Erik2000

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

John Harrison

Erik2000 said:
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
 
E

Erik2000

Hi John!



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



Thanks very much for the tip!



Erik
 
A

Anand

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
 
K

Kevin Goodsell

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
 
G

Greg Schmidt

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!
 
K

Kevin Goodsell

Greg said:
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
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,165
Latest member
JavierBrak
Top