Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9156525
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:47:44+00:00 2026-06-17T12:47:44+00:00

Here is my best shot for a question summary: When multiple separate classes inherit

  • 0

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
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T12:47:45+00:00Added an answer on June 17, 2026 at 12:47 pm

    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.)
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i had questioned here : Best idea for importing text to each NavigationController View
This is similar to the question here: Best practice for working with Foreign key
Just wondering which is best here. I want to output data from a table
I've asked a similar question to this here: Best way to interact with EJBs
Newbie best practices question here; I'll write the code if someone can give me
I searched through here as best I could and though I found some relevant
Here is the best js graph drawing thing - http://thejit.org/static/v20/Jit/Examples/Other/example2.html It is so impressive,
Here are the best implementations I could find for lazy infinite sequences of Fibonacci
I'm wondering about best practice here. Is it good practice for a factory method
I'm looking for best practices here. Sorry. I know it's subjective, but there are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.