struct

C

Carmen Sei

I can represent struct like the following using

{ a, b }

it's similar to list in PERL right?

in perl I can do something like - ( a, b, c )

##########################

typedef struct {
Setup setupID;
const char *setupString;
} SetupCode;

static const SetupCode g_SetupRecordTable[] = {
{GRAPHIC_TEST, "graphic"},
{KEYBOARD_TEST, "keyboard"},
{MY_TEST, "mytest"}
};
 
M

Micah Cowan

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. :)
 

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

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top