Carmen said:
I can represent struct like the following using
{ a, b }
The above cannot be used to represent a struct, only to intialize it
(see below).
it's similar to list in PERL right?
in perl I can do something like - ( a, b, c )
I don't think you'll be well served by thinking of it like a Perl list.
It's rather different. In particular, you can use a Perl list anywhere,
but you can only use brace-enclosed initializers for aggregates.
typedef struct {
Setup setupID;
const char *setupString;
} SetupCode;
static const SetupCode g_SetupRecordTable[] = {
{GRAPHIC_TEST, "graphic"},
{KEYBOARD_TEST, "keyboard"},
{MY_TEST, "mytest"}
};
Yes, that's fine. Note, too, that you're using the { a, b, c }-style
initializers to give values for both an array, and its individual
SetupCode elements.
This sort of initializer is taken from C, and is not available for every
type of C++ struct, but only for what are called "aggregates". Arrays
are one kind of aggregate. Classes (structs) can also be aggregates, but
only when they are very similar to C-style structs (or unions, but
aggregate initialization is pretty useless for unions): they must
contain no user-provided constructors, no access-restricted data members
(except that there may be static members that are private or protected),
must not be derived from other classes, nor use virtual functions. The
only differences from structs in C are that non-virtual function members
are still allowed, and access restriction can be used on _static_ data
members.
Which is fine, because when initializing more complicated C++
structures, what you really want is a good constructor.
