Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » struct inititalization in C and in C++

Credits go to Arthur Kalliokoski, bamccaig, SiegeLord, and Thomas Fjellstrom for helping out!
This thread is locked; no one can reply to it. rss feed Print
struct inititalization in C and in C++
Edgar Reynaldo
Major Reynaldo
May 2007
avatar

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
Member #7,827
October 2006
avatar

ALLEGRO_VERTEX is not opaque.

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

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

bamccaig
Member #7,536
July 2006
avatar

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
Major Reynaldo
May 2007
avatar

Arthur Kalliokoski
Second in Command
February 2005
avatar

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.

They all watch too much MSNBC... they get ideas.

Thomas Fjellstrom
Member #476
June 2000
avatar

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.

--
Thomas Fjellstrom - [website] - [email] - [Allegro Wiki] - [Allegro TODO]
"If you can't think of a better solution, don't try to make a better solution." -- weapon_S
"The less evidence we have for what we believe is certain, the more violently we defend beliefs against those who don't agree" -- https://twitter.com/neiltyson/status/592870205409353730

SiegeLord
Member #7,827
October 2006
avatar

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.

"For in much wisdom is much grief: and he that increases knowledge increases sorrow."-Ecclesiastes 1:18
[SiegeLord's Abode][Codes]:[DAllegro5]:[RustAllegro]

Edgar Reynaldo
Major Reynaldo
May 2007
avatar

Go to: