-4

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
}
D.J.Duff
  • 1,565
  • 9
  • 14
  • it is really hard to understand what you want to achieve. please post some real code and your concrete expectation on what its output should be, or define your problem more formally – Andy Prowl Jan 20 '13 at 15:34
  • As-written I fail to see what `AImpl` and `BImpl` have to do with this question unless you're intentionally setting up an object slicing problem for `foo (class A, class B)`. Since `A` and `B` are unrelated types, it doesn't really matter where they're from. Or i'm entirely not seeing where this is going (and it appears I have company on that). – WhozCraig Jan 20 '13 at 15:36
  • Even with your edit there is no sign of what you actually want to do. – Yakk - Adam Nevraumont Jan 20 '13 at 16:52
  • It seems my explanation is still bad. Here is an attempt to answer Yakk. What I want to do: To hide the use of templates from the user of the library. All the instantiation will be done within the library. That way the user of the library does not have to template his/her code on e.g. Type1 and Type2 or KeypointType and FeatureType in the two examples above. On creation the appropriate type for the template is chosen, and as far as the user is concerned there is no need after that to know anything about the type as it doesn't change the API. – D.J.Duff Jan 21 '13 at 07:16

1 Answers1

1

If I understand you correctly, you are writing a library that uses several, independent, templates (Aimpl, Bimpl, etc.). To hide this fact from the users of the library, you expose only non-templated base-classes of these templates (A, B, etc.).
Now, you have a function foo that needs to do work on two of the templated types, passed in as references to their base classes, and you are facing the problem that you can't (easily) deduce which templates the parameters refer to.

There are only a few options to resolve this dilemma:

  1. Write foo entirely in terms of operations that work on the base classes (as that is all the information that foo has).
  2. Seriously rethink your library design on the merits of using inheritance to hide the templates.
  3. Write the interminable list of dynamic_casts to determine which derive class you are working with. (it is best to avoid this option if in any way possible, because it is a real nightmare.)
Bart van Ingen Schenau
  • 15,488
  • 4
  • 32
  • 41
  • Thank you for your response, and for seeing what I was trying to get at. In considering 1 an additional trade-off is the complexity added to the interface by raising all these operations to that level. – D.J.Duff Jan 20 '13 at 16:40
  • There is also double dispatch techniques, where you define operations on `A` and `B` by requesting that `A` and `B` redispatch it back to you with more type information, once for each of them. – Yakk - Adam Nevraumont Jan 20 '13 at 16:55
  • Thanks for the reference to "double dispatch". This is very helpful. It seems like the common solutions are "visitor pattern" (which unfortunately would require me to template the superclass, which is what I am trying to avoid) or do some degree or other implementing the dispatch mechanism myself as with e.g. http://www.drdobbs.com/double-dispatch-revisited/184405527 – D.J.Duff Jan 21 '13 at 07:09