|
struct inititalization in C and in C++ |
Edgar Reynaldo
Major Reynaldo
May 2007
|
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 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.
My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
SiegeLord
Member #7,827
October 2006
|
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 |
bamccaig
Member #7,536
July 2006
|
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. -- acc.js | al4anim - Allegro 4 Animation library | Allegro 5 VS/NuGet Guide | Allegro.cc Mockup | Allegro.cc <code> Tag | Allegro 4 Timer Example (w/ Semaphores) | Allegro 5 "Winpkg" (MSVC readme) | Bambot | Blog | C++ STL Container Flowchart | Castopulence Software | Check Return Values | Derail? | Is This A Discussion? Flow Chart | Filesystem Hierarchy Standard | Clean Code Talks - Global State and Singletons | How To Use Header Files | GNU/Linux (Debian, Fedora, Gentoo) | rot (rot13, rot47, rotN) | Streaming |
Edgar Reynaldo
Major Reynaldo
May 2007
|
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. My Website! | EAGLE GUI Library Demos | My Deviant Art Gallery | Spiraloid Preview | A4 FontMaker | Skyline! (Missile Defense) Eagle and Allegro 5 binaries | Older Allegro 4 and 5 binaries | Allegro 5 compile guide |
Arthur Kalliokoski
Second in Command
February 2005
|
Edgar Reynaldo said: 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
|
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
Member #7,827
October 2006
|
Edgar Reynaldo said: 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 |
|