Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is there a vector that can store any type?

Reply
Thread Tools

Is there a vector that can store any type?

 
 
Erik2000
Guest
Posts: n/a
 
      08-25-2003

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
 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      08-25-2003

"Erik2000" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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


 
Reply With Quote
 
 
 
 
Erik2000
Guest
Posts: n/a
 
      08-25-2003

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
 
Reply With Quote
 
Anand
Guest
Posts: n/a
 
      08-26-2003
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 <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> Hi John!
>
>
>
> The 'any' type looks like what I am looking for. I will check it out.
>
>
>
> Thanks very much for the tip!
>
>
>
> Erik

 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      08-27-2003
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.

 
Reply With Quote
 
Greg Schmidt
Guest
Posts: n/a
 
      08-27-2003
On Wed, 27 Aug 2003 06:44:26 GMT, "SenderX" <(E-Mail Removed)> 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 ((E-Mail Removed))
Trawna Publications (http://www.trawna.com/)
 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      08-27-2003
Greg Schmidt wrote:

> On Wed, 27 Aug 2003 06:44:26 GMT, "SenderX" <(E-Mail Removed)> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
using vector or other classes to store vector coordinates ssylee C++ 1 01-13-2009 09:56 PM
Initializing vector<vector<int> > and other vector questions... pmatos C++ 6 04-26-2007 05:39 PM
501 PIX "deny any any" "allow any any" Any Anybody? Networking Student Cisco 4 11-16-2006 10:40 PM
Free memory allocate by a STL vector, vector of vector, map of vector Allerdyce.John@gmail.com C++ 8 02-18-2006 12:48 AM



Advertisments