samedi 27 mai 2017

Diamon Inheritance without RTTI

I am designing an object system where all my objects derive from a common base class besides objects I have certain interfaces that I want the objects to implement some objects derive from multiple classes. I am stuck with diamond inheritance problem in that if I make the objects inherit interfaces using virtual public I have to use dynamic_cast to cast from base to derived however the compiler I am using does not have rrti.(embedded compiler for arm) Basically I am looking for workarounds? While searching I have come across the visitor pattern [1] but that seems a bit hacky and I end up with a lot of virtual functions that base class has to know about. I've also come across type erasure something like boost any. My other question is does type erasure works in my case. Currently all my objects are passed around as base pointers in custom shared pointers (again shared_ptr is not available). Then say if the object implements callable interface I static_cast the base pointer to callable pointer and call run method on it. But certain objects needs to implement both callable and iterable that's when the problems arise.

My main targets are C++11 and gcc/clang.

[1] C++ - downcasting a diamond shape inherited object without RTTI/dynamic_cast

Aucun commentaire:

Enregistrer un commentaire