struct inititalization in C and in C++
Edgar Reynaldo

Hey, I have this code which won't compile. The first half I guess would work in C, but it does not work in C++. The second way almost works, but the order of the parameters is wrong and since ALLEGRO_VERTEX is an opaque structure it might change, and I don't know the correct order anyway.

/*
   ALLEGRO_VERTEX quad[4] = {// counterclockwise from top left
      {.x = viewdrawx             , .y = viewdrawy              , .z = 0.0 , .color = white , .u = texlx , .v = texty},
      {.x = viewdrawx             , .y = viewdrawy + viewheight , .z = 0.0 , .color = white , .u = texlx , .v = texby},
      {.x = viewdrawx + viewwidth , .y = viewdrawy + viewheight , .z = 0.0 , .color = white , .u = texrx , .v = texby},
      {.x = viewdrawx + viewwidth , .y = viewdrawy              , .z = 0.0 , .color = white , .u = texrx , .v = texty}
   };
//*/
   ALLEGRO_VERTEX quad[4] = {// counterclockwise from top left
      {viewdrawx             , viewdrawy              , 0.0 , {white} , texlx , texty},
      {viewdrawx             , viewdrawy + viewheight , 0.0 , {white} , texlx , texby},
      {viewdrawx + viewwidth , viewdrawy + viewheight , 0.0 , {white} , texrx , texby},
      {viewdrawx + viewwidth , viewdrawy              , 0.0 , {white} , texrx , texty}
   };

What's the best way to initialize an opaque C struct in C++?

Edit
I like this option :

Quote:

Option D:

FooBar FooBarMake(int foo, float bar)

Legal C, legal C++. Easily optimizable for PODs. Of course there are no named arguments, but this is like all C++. If you want named arguments, Objective C should be better choice.

SiegeLord

ALLEGRO_VERTEX is not opaque.

This should be ok: {x, y, z, u, v, color}.

bamccaig

As for opaque types, IMO there needs to be a public API, or else you aren't supposed to touch it. You should never directly access an opaque structure. Your only access to it should be through the API.

Edgar Reynaldo
SiegeLord said:

ALLEGRO_VERTEX is not opaque.

How do you know when a struct is opaque?

Quote:

This should be ok: {x, y, z, u, v, color}.

The manual documents them in the order x, y, z, color, u, v. It's kind of misleading.

Arthur Kalliokoski

How do you know when a struct is opaque?

If it were truly opaque, there'd be no include file available to user programs to say what the members were. Some interfaces aren't really opaque, such as stdio, but if you don't treat them as such you're in for a lot of pain.

Thomas Fjellstrom

Allegro has some opaque types. the header only declares:

struct ALLEGRO_FOO;

so you can't access anything inside it. An internal header will define the structure for allegro internal code.

SiegeLord

How do you know when a struct is opaque?

If this code compiles: Struct a; then Struct is not opaque.

Quote:

The manual documents them in the order x, y, z, color, u, v. It's kind of misleading.

This has now been fixed.

Edgar Reynaldo

Thanks for fixing the docs.

Thread #613599. Printed from Allegro.cc