On Github WojciechMigda / cpp-interface-classes-presentation
Created by Wojciech Migda
class I { public: virtual void foo(void) = 0; };
destructor?
destructor?
constructor?
constructor?
copy operator?
copy operator?
class I { public: // default constructor virtual void foo(void) = 0; virtual ~I(void){} private: I& operator=(const I &); };
class D : public I { //[access-specifier]: virtual void foo(void){} };
destructor?
destructor?
constructor?
constructor?
interface methods' access?
interface methods' access?
class D : public I { public: D() : I() {} virtual ~D(void){} private: virtual void foo(void){}; };
where?
where?
what should it return?
what should it return?
std::unique_ptr<IFileObject> createFileObject(const std::string & in_path);we can do better than that..
class IFileObject { public: typedef std::unique_ptr<IFileObject> ptr; ... }; IFileObject::ptr createFileObject(const std::string & in_path);
Boundaries of the interface will be emphasized by using a struct of function pointers.
struct tagListInterface { int (* const Add)(List * L, const void * newval); int (* const Clear)(List * L); int (* const Contains)(const List * L, const void * element); //... };
Interface structure is either a global variable or its pointer is returned by a factory function.
struct tagListInterface const * iList(void); //... iList()->Clear(list_ptr);
We can even introduce polymorphism and private interface by means of VTable pointer:
struct _List { ListInterface * VTable; list_element * Last; list_element * First; //... };
void test_1(void) { struct Mock { static int empty(ListCPtr self) { return 0; } } mock; struct ListInterface interface = {0, 0, mock.empty, 0}; ListPtr list_p = give_random_list(); list_p->vTable = &interface; // do the test }
int empty(ListCPtr self) { int result; if (self->vTable->empty && self->vTable->empty != empty) { result = self->vTable->empty(self); } else { result = empty_impl(self); } return result; }