[comp.lang.c++] initializing a struct

cs00jec@unccvax.uncc.edu (Jim Cain) (10/15/90)

I am trying to define a structure array for a static set of data. In C I
would declare the following:

struct {
   char *name;
   int i;
} list [] = {
   "namezero", 5,
   "nameone", 1,
   ...
   "namen", 16
};

This will be used by a parser as a look-up table for command names and
other information which belongs to each command.

This is identical to C code I have written that works. My question is --
Is something this simple possible in C++? It seems that C++ considers
structs and unions as classes. Can you initialize class object arrays
like this? I really don't want the overhead of using pointers (such as
a linked list representation) because there is no reason for this data
to be dynamic (unless, of course, that's the only way to do it).

rusbara2@sage.cc.purdue.edu (Bob Rusbasan) (10/15/90)

In article <2840@unccvax.uncc.edu> cs00jec@unccvax.uncc.edu (Jim Cain) writes:
>
>I am trying to define a structure array for a static set of data. In C I
>would declare the following:
[ example of typical structure initialization deleted ]

>This will be used by a parser as a look-up table for command names and
>other information which belongs to each command.

>This is identical to C code I have written that works. My question is --
>Is something this simple possible in C++? It seems that C++ considers
>structs and unions as classes. Can you initialize class object arrays
>like this? I really don't want the overhead of using pointers (such as
>a linked list representation) because there is no reason for this data
>to be dynamic (unless, of course, that's the only way to do it).

I remember having tons of fun when I first tried to do this.  What would
be nice is if you could do this

[declaration of foo]

foo name[] = {
	{ 0, 5, "name"},
	{ 7, 4, "another_name" }
	[ ... ]
};

and the construction matching the arguments in brackets would be call
for each member of the array of foo.  Maybe this seems arrogant to
others, but just by the fact that this is what seemed like the OBVIOUS
way to do it makes me wonder why it's not in the language.  I thought
one of the big things of C++ was defining your own data types and
using them like any other types, but with more power.  It seems to me
that with structs a "default constructor" is called with the values in
brackets, so why not extend that to user-definable structures?  Plus
they could call different contructors...

Anyway, here's what you have to do (unless I missed something; Lord 
knows I tried, and this does work):

foo name[] = {
	foo ( 0, 5, "name"),
	foo ( 7, 4, "another_name" )
	foo [ ... ]
};

As I said, just by the virtue of it being non-obvious seems to indicate
that something's wrong.  In C when you declare an arry of struct foo
each element if, of course, a struct foo in itself, but you don't have
too tell it that just like you don't have to explicitly tell it that
each element in an array ints is an int.

That's my opinion at least.  I suppose I should go don my asbestos suit
now...

-----
Bob Rusbasan
bob@en.ecn.purdue.edu

vaughan@mcc.com (Paul Vaughan) (10/15/90)

   From: rusbara2@sage.cc.purdue.edu (Bob Rusbasan)

   In article <2840@unccvax.uncc.edu> cs00jec@unccvax.uncc.edu (Jim Cain) writes:
   >
   >I am trying to define a structure array for a static set of data. In C I
   >would declare the following:
   [ example of typical structure initialization deleted ]

   >This will be used by a parser as a look-up table for command names and
   >other information which belongs to each command.

   >This is identical to C code I have written that works. My question is --
   >Is something this simple possible in C++? It seems that C++ considers
   >structs and unions as classes. Can you initialize class object arrays
   >like this? I really don't want the overhead of using pointers (such as
   >a linked list representation) because there is no reason for this data
   >to be dynamic (unless, of course, that's the only way to do it).

   I remember having tons of fun when I first tried to do this.  What would
   be nice is if you could do this

. . .

   Anyway, here's what you have to do (unless I missed something; Lord 
   knows I tried, and this does work):

   foo name[] = {
	   foo ( 0, 5, "name"),
	   foo ( 7, 4, "another_name" )
	   foo [ ... ]
   };

Bob gave a reasonable answer, but seems to have missed that the
original code posted by Jim also works fine (and is simpler to some
people's minds).

#include <stream.h>

struct {
   char *name;
   int i;
} list [] = {
   "namezero", 5,
   "nameone", 1,
   "namen", 16
};

main() {
  for(int i = 0; i < 3; i++)
    cout << list[i].name << " " << list[i].i << "\n";
}

bash$ !g
g++ -o trash trash.cc
bash$ trash
namezero 5
nameone 1
namen 16
bash$ !C
CC -o trash trash.cc
CC  trash.cc:
cc   -o /d2/prism/prism/src/sim/src/trash -I/net/sunspot/usr/cadsw/CC/sun4/incl  trash.c  -L/net/sunspot/usr/cadsw/CC/sun4/ -lC
bash$ trash
namezero 5
nameone 1
namen 16


 Paul Vaughan, MCC CAD Program | ARPA: vaughan@mcc.com | Phone: [512] 338-3639
 Box 200195, Austin, TX 78720  | UUCP: ...!cs.utexas.edu!milano!cadillac!vaughan