Based on Eric Finn's Answer , you can simply declare an interface class to store all of your public methods that are considered your API, and hide all implementations and private members / methods in the implementation class that inherits from the interface class, here is an example:
Your header file: my_api.h
// your API in header file // my_api.h class interface { public: static interface* CreateInstance(); virtual void draw() = 0; virtual void set(int) = 0; };
your implementation (shared library): my_api.cpp (users will not see this when you make it a shared library) This way you can hide all your implementations and private methods / members here.
#include "my_api.h" // implementation -> in .cc file class implementation : public interface { int private_int_; void ReportValue_(); public: implementation(); void draw(); void set(int new_int); }; implementation::implementation() { // your actual constructor goes here } void implementation::draw() { cout << "Implementation class draws something" << endl; ReportValue_(); } void implementation::ReportValue_() { cout << "Private value is: " << private_int_ << endl; } void implementation::set(int new_int) { private_int_ = new_int; } interface* interface::CreateInstance() { return new implementation; }
How the user uses your API:
#include <iostream>
Output:
Implementation class draws Private int is: 2 Implementation class draws Private int is: 1
In this template, your api is just an abstract class that works like a factory, you can also implement a virtual method in different classes and specify which instance you want to call.
Charles Chow
source share