Is there a way in C++ to construct your class such that given a pointer to your class you can instruct dynamic_cast<>() how to cast to another class for which you are wrapping the implementation? Would operator cast do the trick? Imagine I have an Abstract interface base class and derive a concreteA from this as well as concreteB, but concreteB wraps the interface to an object of type concreteA. If I get a request to cast to concreteA from concreteA, I'd like it to work:
class Abstract {
public:
virtual void interface() = 0;
};
class concreteA : public Abstract {
public:
virtual void interface();
};
class concreteB : public Abstract {
public:
concreteB(concreteA &underlying)
: _underlying(&underlying) {
}
virtual void interface();
operator concreteA*() {
return _underlying;
}
private:
concreteA *_underlying;
};
void
myTest() {
concreteA myClassA;
concreteB myClassB(myClassA);
Abstract *abstract = &myClassB;
concreteA *underlying = dynamic_cast<concreteA *>(abstract);
}