T.Zak T.Zak - 1 year ago 113
C++ Question

is it possible to use dynamic cast in an interface like architecture in c++

I m kinda new in c++ and I had this problem:

Is it possible to access the *parent pointer in class Object with having a:

class Object (*parent ptr here pointing to the parent or nullPtr if it is root)
class IStorage <- which contains the ability of an object to nest children
class ISearch <- which contains the ability to search in a given container
class Container : public virtual Object,public virtual IStorage,public virtual Isearch <- resulting class

Now in Istorage I have a method
virtual void addChild(T* obj)
which when adding a child should update the *parent of the child but I dont know if I can access it.

Is it possible to get the pointer to the Object from the IStorage
Is this architecture bad? Should I just opt for a generic Tree like architecture?

Thanks in advance

Answer Source

I am afraid There is no hack-free way, what you could do is to add to IStorage a virtual Object* getObject() = 0; and define it inside Container as such : Object* getObject() override { return this; }.

Another way (more OOP friendly in my opitnion) would be to add to IStorage a virtual method like : virtual void updateAfterAddChild() = 0; and implement it inside Container, which would call appropriate method on Object class.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download