Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Array can contain two types?

2 views
Skip to first unread message

Fred Rathke

unread,
Nov 12, 2002, 5:12:17 AM11/12/02
to
Hello,

is there any way to do this?

#define DIM 0

//how to declare var[][] here?

// define or init var
var[DIM][0] = "DIM01";
var[DIM][1] = 5;


I never saw something like this my days ;-) Or must I do it like this?
...

#define DIM 0

varname[DIM] = "DIM01";
varamount[DIM] = 5;

Thanks a lot for any advice how to start right.

Sincerely yours
Fred Rathke
(newbie)

Jakob Bieling

unread,
Nov 12, 2002, 5:53:57 AM11/12/02
to
"Fred Rathke" <com...@hotmail.com> wrote in message
news:aqqk5u$thk$06$1...@news.t-online.com...

> Hello,
>
> is there any way to do this?
>
> #define DIM 0
>
> //how to declare var[][] here?
>
> // define or init var
> var[DIM][0] = "DIM01";
> var[DIM][1] = 5;
>
>
> I never saw something like this my days ;-) Or must I do it like this?
> ...
>
>
>
> #define DIM 0
>
> varname[DIM] = "DIM01";
> varamount[DIM] = 5;


Use a struct:

struct var
{
std::string name;
int amount;
};

var vars[DIM];
vars.name = "DIM01";
vars.amount = 5;

hth
--
jb

(replace y with x if you want to reply by e-mail)


Karl Heinz Buchegger

unread,
Nov 12, 2002, 5:58:37 AM11/12/02
to

Fred Rathke wrote:
>
> Hello,
>
> is there any way to do this?
>
> #define DIM 0
>
> //how to declare var[][] here?
>
> // define or init var
> var[DIM][0] = "DIM01";
> var[DIM][1] = 5;
>

no way. The elements of an array must be of the same type.

> I never saw something like this my days ;-) Or must I do it like this?
> ...
>
> #define DIM 0
>
> varname[DIM] = "DIM01";
> varamount[DIM] = 5;
>
> Thanks a lot for any advice how to start right.
>

Where is your textbook. Somewhere in the first sections it will
talk about structures.

struct MyData
{
std::string Label;
int Amount;
};

MyData Variables[10];

Variables[0].Label = "Hugo";
Variables[0].Amount = 5;

Variables[1].Label = "Test";
Variables[1].Amount = 2;


You really should buy a book. You can't learn the language and how to use
it efficently without one. Even if you know VB.

--
Karl Heinz Buchegger
kbuc...@gascad.at

Jakob Bieling

unread,
Nov 12, 2002, 6:06:06 AM11/12/02
to
"Jakob Bieling" <net...@gmy.net> wrote in message
news:aqqmi6$eok$02$1...@news.t-online.com...

> var vars[DIM];
> vars.name = "DIM01";
> vars.amount = 5;

Should be:

var vars[DIM];
vars[0].name = "DIM01";
vars[0].amount = 5;

sorry :)

Vincent Finn

unread,
Nov 12, 2002, 6:18:27 AM11/12/02
to
Fred Rathke wrote:
> Hello,
>
> is there any way to do this?
>
> #define DIM 0
>
> //how to declare var[][] here?
>
> // define or init var
> var[DIM][0] = "DIM01";
> var[DIM][1] = 5;
>

go to
http://www.boost.org
and check the 'any' class

Vin

Peter van Merkerk

unread,
Nov 12, 2002, 8:10:16 AM11/12/02
to
No, arrays can only store one type. However I don't think that is what want
or need anyway.

> #define DIM 0

If DIM is the dimension of the array it should be at least 1 and most likely
more.

> // how to declare var[][] here?


> // define or init var
> var[DIM][0] = "DIM01";
> var[DIM][1] = 5;

Assuming var[DIM][0] and var[DIM][1] are related somehow, a struct or class
to group information would be a more appropriate choice:

struct MyStruct
{
std::string str;
int num;
};

MyStruct var[DIM];

var[0].str = "DIM01";
var[0].num = 5;
var[1].str = "DIM02";
var[1].num = 6;

If you really have to store multiple types in a single container (which can
be but is limited to an array) you basically have two options:

1) Put in the container objects of a class that can hold multiple types
(e.g. the "any" class in the Boost library www.boost.org).

2) Define a common base class, from which specific classes are derived. In
the container store base class pointers. You have to consider lifetime and
ownership issues which this solution however.

Considering the example code you provided, I recommend that you buy and read
a good C++ beginners book. You can find recommendations for those at
www.accu.org.

BTW: In many cases std::vector is preferable to a C style array, if your
are coding C++ you might as well take advantage of the features it offers.

--
Peter van Merkerk
merkerk(at)turnkiek.nl


-wiseguy

unread,
Nov 12, 2002, 4:59:08 PM11/12/02
to
"Fred Rathke" <com...@hotmail.com> wrote in news:aqqk5u$thk$06$1@news.t-
online.com:

> Hello,
>
> is there any way to do this?
>
> #define DIM 0
>
> //how to declare var[][] here?
>
> // define or init var
> var[DIM][0] = "DIM01";
> var[DIM][1] = 5;
>

yes, but you must either use variant records implemented via "unions" and
then explicitely track the type of data in each element, or work out a
convoluted hack of polymophism via virtual access methods. Neither method is
pretty, and trying to do what you are asking probably means you need to
reevaluate your design idea. If you're simply trying to make generic
containers then use the STL ones, but they still only allow a single type in
each container unless doing what I mentioned above. Your inquiry probably
reflects a flaw your design procedure.

--
-- Rob Prowel (A.K.A. da wiseguy)
URL: http://www.prowel.com/

-----= Posted via Newsfeeds.Com, Uncensored Usenet News =-----
http://www.newsfeeds.com - The #1 Newsgroup Service in the World!
-----== Over 80,000 Newsgroups - 16 Different Servers! =-----

Alexander Terekhov

unread,
Nov 12, 2002, 5:27:39 PM11/12/02
to
0 new messages