使用继承来重定向多个并行层次结构的c++
Use inheritance to redirect with multiple parallel hierarchies C++
这是我最好的问题总结:
当多个独立的类从多个独立的基类继承每个类时类,我如何使用继承机制来写一个函数从多个这些基类的对象作为参数?
但是最好用例子来解释。
我有一个库,在其API中提供以下类:
class A{..};
class B{..};
这些类的存在是为了向使用模板的应用程序隐藏模板的复杂性。实现涉及模板:
template <Type1>
class AImpl: public A{..};
template <Type2>
class BImpl: public B{..};
问题是我需要一个这样的函数:
void foo(A insta,B instb);
继承机制在这里似乎没有多大帮助,因为如果函数在AImpl
内部,它就没有办法为BImpl
自动选择正确的Type2
(没有动态强制转换列表)。
到目前为止,我的最佳解决方案是将其中一个类模板化两次:
template <Type1,Type2>
class BImpl: public B{
void foo(A insta);
};
但是这种方法似乎并没有扩展到将A
和B
与几个任意模板化实例结合起来可能有用的情况(这需要一个动态强制转换,仅在确定参数insta
实际上是AImpl<Type2>
或前面提到的强制转换列表的情况下才有效)。
如果不给A
和B
的用户增加复杂性,是否有可能做到我在这里尝试做的事情,或者是否有更习惯的方法?
谢谢大家。
编辑
根据Bart van Ingen Schenau的回答,这可能无关紧要,但是为了回应Nawaz和Andy Prowl的询问,我制定了以下示例文件。它需要PCL库,但它是工作代码(虽然是我想要实现的一个减少的例子)。
感谢大家的意见。
类Features
类似于上面的A
, Keypoint
类似于上面的B
。我也在问题中添加了PCL标签。
#include <pcl/features/fpfh.h> //gives pcl::PointCloud, pcl::FPFHSignature33, etc.
//interface
class Keypoints{
public:
virtual unsigned int size();
//more virtual methods here
};
class Features{
public:
virtual unsigned int size();
//more virtual methods here
};
//implementation
template<typename KeypointType>
class KeypointsImpl:public Keypoints{
public:
typename pcl::PointCloud<KeypointType>::Ptr keypoints_;
virtual unsigned int size(){ return keypoints_->size();}
//more implementations of virtual methods here
};
template<typename FeatureType>
class FeaturesImpl:public Features{
public:
typename pcl::PointCloud<FeatureType>::Ptr features_;
virtual unsigned int size(){ return features_->size();}
//more implementations of virtual methods here
};
//void removeBadFeatures(Keypoints k,Features f); //<-- would like to have this too
int
main (int argc, char ** argv)
{
//Class construction could be done anywhere.
Features *f = new FeaturesImpl<pcl::FPFHSignature33>();
Keypoints *k = new KeypointsImpl<pcl::PointXYZ>();
int a = f->size();
int b = k->size();
//removeBadFeatures(k,f); //will alter f and k in concert
}
如果我理解正确的话,您正在编写一个使用几个独立模板(Aimpl
, Bimpl
等)的库。为了对库的用户隐藏这一事实,您只公开这些模板(A
、B
等)的非模板化基类。
现在,您有一个函数foo
,它需要在两个模板化类型上工作,作为对其基类的引用传入,并且您面临着无法(容易地)推断参数引用的模板的问题。
只有几个选项可以解决这个难题:
- 完全按照在基类上工作的操作来写
foo
(因为这是foo
拥有的所有信息)。 - 在使用继承隐藏模板的优点上认真地重新考虑你的库设计。
- 编写
dynamic_cast
s的冗长列表,以确定您正在使用哪个派生类。(如果可能的话,最好避免这个选项,因为这是一个真正的噩梦。)
相关文章:
- 如何重构类层次结构以避免菱形问题
- C++ 中模板化类型的类层次结构
- 为什么不同类型层次结构的指针之间的dynamic_cast定义得很好?
- 继承层次结构并将元素添加到向量
- C++ 类层次结构中的"对齐"是什么意思?
- 相同的层次结构,访问基类的受保护成员时的行为不同
- 类层次结构中的运算符重载
- 如何在层次结构中实现运算符使用?
- 反向层次结构中的可变参数模板参数
- 如何在继承层次结构中调用具有默认参数的构造函数?
- C++ 提升 - 包含类层次结构对象的类的序列化
- 在C++继承层次结构时提取实现者
- 在C++中将类实例添加到对象层次结构中的问题
- 确定大层次结构中基本指针的实际类型,无需dynamic_cast
- 在继承层次结构中复制和移动
- 模板冲突的类型-但类型应该是相同的cfr类层次结构
- 删除父/子窗口层次结构的最佳方法
- 是否可以使一个类成为两个不同层次结构的子类?
- 通过并行继承层次结构中的父级关联子项
- 使用继承来重定向多个并行层次结构的c++