是否可以在不同类型的原始指针A *和B *之间定义自定义转换器(converter1< T>和converter2),
然后在某个类中创建所有函数(fa()和fb())
使用适当的转换器(converter1< T>或converter2)?
然后在某个类中创建所有函数(fa()和fb())
使用适当的转换器(converter1< T>或converter2)?
简而言之,我希望程序使用我的自定义函数将A *转换为B *,反之亦然.
我希望它能自动完成,以方便我.
- class Manager{
- void fb(B* b){ /** something complex */ }
- void fa(A* a){ /** different thing complex */ }
- void testCase(){
- A* a= ... ;
- fa(a);
- fb(a); //automatic convert to B* using "converter2" (wish)
- B* b= ... ;
- fa(b); //automatic convert to A* using "converter1" (wish)
- fb(b);
- }
- template<class T> T* converter1(B* b){ //hardcoded,non-static.
- return this->getId<T>(b);
- //^^^ just an example to show how custom it is,// currently T=A
- }
- B* converter2(A* a){ //hardcoded
- return a->getB();
- //^^^ just an example to show how custom it is.
- }
- }
真实情况有很多A – A1,A2,A3等.
A和B不是相互派生的.
我希望有办法.我想到指针的构造函数.