Jump to content
  • Advertisement
Sign in to follow this  
Alpha_ProgDes

Is there a Pimpl idiom for C?

This topic is 3762 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I see that there's one for C++. I've done some Googling but haven't seen any for C. If someone has an example or link that would be great.

Share this post


Link to post
Share on other sites
Advertisement
The way to do it in C is to declare an opaque struct in the header file and hide the implementation in the C file. This has two effects:

1. The definition of the structure is hidden in the C file so only that file needs to be recompiled if the structure changes. This is what the pimpl idiom is good for.

2. The variables has been made private so the data is encapsulated.

Many C API's does this. For example Lua.


// Gadget.h

// Declare the struct to allow client code to keep pointers to a Gadget.
typedef struct Gadget Gadget;

Gadget* GadgetCreate();
void GadgetDestroy(Gadget*);
int GadgetDoStuff(Gadget*);


// Gadget.c

// Define the struct
struct Gadget
{
};

// Implement the functions

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!