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

jmasterx
  • 52,639
  • 96
  • 311
  • 557
  • 2
    Just because intellisense doesn't show anything doesn't mean they're empty. You may just not have the source code in the project. – JoshD Oct 05 '10 at 00:42
  • 1
    I don't know Visual Studio very well, but in my usage I've tentatively concluded that forward declaring a class/struct (i.e. `struct REAL_ALLEGRO_BITMAP;` without the {...} body) inhibits intellisense in this way. I'm sure other readers can confirm/rebuke... – Tony Delroy Oct 05 '10 at 00:50
  • IntelliSense (at least in VS2010) only shows members which are actually available at the given point in the source code. Naturally, if you only have a forward declaration without a definition, you won't get any members listed, even if a full definition is available in some other .cpp – Pavel Minaev Oct 05 '10 at 01:12

2 Answers2

5

What you're looking at is an opaque pointer or opaque data type (link and link). Here's an SO thread discussing these: What is an opaque value?

Community
  • 1
  • 1
Richard Cook
  • 32,523
  • 5
  • 46
  • 71
0

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
  • 43,959
  • 6
  • 69
  • 99
  • Why `#define` and not `typedef`? – Pavel Minaev Oct 05 '10 at 01:13
  • 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 Oct 05 '10 at 01:24