Skip to content

Instantly share code, notes, and snippets.

@NocturnDragon
Forked from dwilliamson/Pimpl.cpp
Created April 17, 2017 16:58
Show Gist options
  • Save NocturnDragon/e9b36cee880be611e402435bd162d6e7 to your computer and use it in GitHub Desktop.
Save NocturnDragon/e9b36cee880be611e402435bd162d6e7 to your computer and use it in GitHub Desktop.
How I Pimpl
struct Data
{
Data()
{
}
~Data()
{
}
// NO methods - this is a pure data type
// List of properties
int x;
void* resource;
};
// Local static for all implementation details
namespace
{
// This would normally have to be in your class private section
void Detail(PrivateImplementation* data)
{
}
}
// Limited boiler-plate
API::API()
{
data = new Data();
}
API::~API()
{
delete data;
}
void API::DoStuff()
{
// Write most of the implementation in here, branching out to local statics
// for any further detail implementations
data->resource;
data->x;
// Chain to other functions
Detail(data);
}
class API
{
public:
// Use new/delete to control lifetime
API();
~API();
void DoStuff();
private:
struct Data* data;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment