#include <iostream>
// begin by making interfaces
// for per class acces to class A
class interface_for_class_b
{
public:
virtual void method_for_class_b() = 0;
};
class interface_for_class_c
{
public:
virtual void method_for_class_c() = 0;
};
// then inherit class from those interfaces
// but keep the implementations private
// this hides them from everyone
class A :
public interface_for_class_b,
public interface_for_class_c
{
public:
void some_public_method()
{
std::cout << "A::some_public_method()\n";
}
private:
void method_for_class_b() override
{
std::cout << "A::method_for_class_b()\n";
};
void method_for_class_c() override
{
std::cout << "A::method_for_class_c()\n";
};
};
// then use dependency injection
// for class B with interface_for_class_b
class B
{
public:
explicit B(interface_for_class_b& a) :
m_interface_to_a{ &a }
{
}
void some_method()
{
std::cout << "B::some_method()\n";
// but now b can acces "private" function
// through the explicit interface
m_interface_to_a->method_for_class_b();
}
private:
interface_for_class_b* m_interface_to_a;
};
class C
{
public:
explicit C(interface_for_class_c& c) :
m_interface_to_a{ &c }
{
}
void some_method()
{
std::cout << "C::some_method();\n";
m_interface_to_a->method_for_class_c();
}
private:
interface_for_class_c* m_interface_to_a;
};
int main()
{
A a;
B b{ a }; // inject dependency to give b acces to "private"functions
C c{ a };
a.some_public_method();
// a.method_for_class_b(); // is not accesibe, it is private
b.some_method();
c.some_method();
return 0;
}