在C++动态转换,我想能够做到以下几点:从封装接口
struct IWrapper {
template<typename U>
U* dynamic_cast_to() { ??? } // what to do here?
};
template<typename T>
struct Wrapper : IWrapper {
Wrapper(T* _p) :p(_p) {}
T* p;
};
有了这个,我希望能够做到
SomeDerived *a = new SomeDerived;
IWrapper *x = new Wrapper<SomeDerived>(a);
SomeBase *b = x->dynamic_cast_to<SomeBase>()
dynamic_cast_to()
应该返回一个指针,如果确实SomeDerived
继承SomeBase
和NULL
,如果不是,正常dynamic_cast
的工作方式相同。
这甚至可能吗?
这将无法正常工作。能够将'dynamic_cast'' SomeDerived'转换为'SomeBase'并不意味着您可以''包装器''转换为'包装器'。这就是整个问题。 –
shoosh
2013-02-24 17:35:46
您要求将'Wrapper'转换为'SomeBase',而不是'包装'。你是什么意思? –
Useless
2013-02-24 17:43:54
嘿2个赞成票是什么?任何downvoter照顾解释?他们在我看来很不合适。 – 2013-02-24 20:09:10