In C ++ you can use inheritance:
// User view struct Image; // forward declaration (inclomplete type). Image* LoadFromFile (...); // You can use pointer to incomplete type // Implementation view struct Image: SDL_Surface { }; // here you go !! :-)
Note: it would be safer to use classes and personal inheritance, so that only the image knows that it is SDL_Surface.
In some cases, it would be undesirable to inherit from an existing implementation class (for example, if you need a virtual destructor and the base class is not). Then PIMPL idiom can be an alternative (at the cost of additional indirectness):
//User View unchanged struct Image; int TestImage(Image*z); //implementation view struct Image { struct ImageImpl { int x; }; // nested definition or typedef or whatever ImageImpl *p; // works in every case, at cost of an extra indirection instead of a pointer }; int TestImage(Image* z) { return z->p->x; }
The main advantage of PIMPL here is that you can expose more than just an incomplete type, and therefore offer your clients some useful member functions. But if you do not need it, and when you are already working with poitners on an object from the client side, you can also go directly to the composition and have an ImageImpl element instead of a PIMPL pointer.
In C, you cannot use inheritance. But the composition would certainly do the trick:
struct Image { SDL_Surface s; };
source share