Skip to content

Instantly share code, notes, and snippets.

@mjschutz
Last active October 31, 2015 23:13
Show Gist options
  • Save mjschutz/91b9cde5aecdfedaf5e6 to your computer and use it in GitHub Desktop.
Save mjschutz/91b9cde5aecdfedaf5e6 to your computer and use it in GitHub Desktop.
Hidden Interface Implementation on C++
#include <iostream> // std::cout
class Interface {
public:
class HiddenInterface;
static inline Interface* of(Interface::HiddenInterface* hInterface);
static inline Interface& of(Interface::HiddenInterface& hInterface);
virtual void show(char const* const msg) const =0;
virtual ~Interface() {}
};
class Interface::HiddenInterface: private Interface
{
public:
friend Interface* Interface::of(Interface::HiddenInterface* hInterface);
friend Interface& Interface::of(Interface::HiddenInterface& hInterface);
virtual ~HiddenInterface(){}
};
Interface* Interface::of(Interface::HiddenInterface* hInterface)
{
return hInterface;
}
Interface& Interface::of(Interface::HiddenInterface& hInterface)
{
return hInterface;
}
class ObjClass: public Interface::HiddenInterface {
void show(char const* const msg) const {
std::cout << msg << std::endl;
}
public:
virtual ~ObjClass() {}
// Implement public methods
};
int main()
{
ObjClass object;
Interface::of(object).show("Hello, world!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment