tags:

views:

102

answers:

2

I notice in several API's, that you may create a struct which is used internally and has nothing. ex:

ALLEGRO_BITMAP *bmp;

...
bmp->(intellesense shows nothing)

how do these types of structures work? are they simply handled like this internally?

REAL_ALLEGRO_BITMAP *realbmp = (REAL_ALLEGRO_BITMAP*)bmp;

or is there a cleaner solution?

Thanks

+5  A: 

What you're looking at is an opaque pointer or opaque data type (link and link). Here's an SO thread discussing these: http://stackoverflow.com/questions/3854113/what-is-an-opaque-value

Richard Cook
A: 

Simply put, a "hidden" structure is a structure that you have a declaration for but no definition. In the case where an API provides this type of structure, this means that you shouldn't have to worry about what the internals of the structure look like. Giving you the declaration provides enough information so that you can create pointers to that structure type, which is usually sufficient when using the associated API.

In your case, the internals of the API probably have a definition that looks like this:

struct allegro_bitmap {
    /* Insert mystery internals here */
};
#define ALLEGRO_BITMAP struct allegro_bitmap

Somewhere in one of the headers that you are given, you have only a simple declaration:

struct allegro_bitmap;
#define ALLEGRO_BITMAP struct allegro_bitmap

This is enough information for the compiler and linker to build your code and link it to the associated library. In order to do anything useful with the pointer, you have to use the API functions (since they have the full definition of the structure).

bta
Why `#define` and not `typedef`?
Pavel Minaev
I've seen it done both ways. Typically, I've seen the `typedef` form used when the API uses `struct allegro_bitmap` internally and wants to provide a simpler interface to users (the user doesn't have to care if it is a simple data type or structure). In that case, the macro form can be a bit cleaner than the `typedef` form. It's more a matter of personal preference than anything else.
bta