Here is my best shot for a question summary:
When multiple separate classes inherit each from multiple separate base classes, how to I use the inheritance machinery to write a function that takes objects from multiple of these base classes as parameters?
But better to explain by example.
I have a library that supplies the following classes in its API:
class A{..};
class B{..};
These classes are there to hide the complexity of templates from the using application. The implementation involves templates:
template <Type1>
class AImpl: public A{..};
template <Type2>
class BImpl: public B{..};
The problem is that I need a function like:
void foo(A insta,B instb);
The inheritance machinery doesn't seem to help me here much since if the function is inside AImpl
there is no way for it to choose automatically the right Type2
for BImpl
(without a list of dynamic casts).
My best solution so far is to template one of the classes twice:
template <Type1,Type2>
class BImpl: public B{
void foo(A insta);
};
But this approach does not seem to extend to the situation where it might be useful to combine A
and B
with several arbitrarily templated instantiations (this needs a dynamic cast that would only work where it was for sure that the parameter insta
was actually an AImpl<Type2>
- or the aforementioned list of casts).
Without adding complexity to the user of A
and B
, is it possible to do what I'm trying to do here, or is there a more idiomatic approach?
Thanks to all.
Edit
This may be irrelevant in light of the answer by Bart van Ingen Schenau, but in response to the queries of Nawaz and Andy Prowl, I have formulated the following example file. It needs the PCL library, but it is working code (though a cut down example of what I am trying to achieve).
Thanks to everyone for your input.
The class Features
is analogous to A
above and Keypoint
to B
above. I have added the PCL tag to the question too.
#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
}