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 927629
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T19:53:01+00:00 2026-05-15T19:53:01+00:00

I have a number of class, all with exactly the same interface. This interface

  • 0

I have a number of class, all with exactly the same interface. This interface defines a few methods, some of which are templated (the class itself may or may not be).

So the interface looks something like this

class MyClass
{
public:
  void Func1();

  template <typename T>
  void Func2(T param);
};

I have a number of functions which take various objects which conform to this interface but want to avoid having to know the exact implementation at compile time.

Obviously, the default C++ solution would be to have a base type that all these classes derive from and pass around a pointer to that and have polymorphism do all the work.

The problem is that templated member functions cannot be virtual so this method cannot be used. I also want to avoid changing the current set of classes that follow this interface because there are a large number of them, some of which are defined outside the scope of my project.

The other solution is to template the functions that use these objects so they specialise for the right type. This could be a solution but due to legacy requirements templating a large number functions may not be possible (this is something I cannot do anything about as the client code isn’t something I have responsibility for).

My initial thought was to provide some kind of carrier class which is type neutral and in effects wraps the common interface here and has a base interface class to pass around the internal type.

Something along the lines of

class MyInterface
{
public:   
 virtual void Func1() = 0;
};

template <typename T>
class MyImplementation
{
public:
  virtual void Func1()
  {
    m_impl->Func1();
  }

private:
  T* m_impl;
};

But again the templated member functions seem to block this approach.

I looked at the boost::any and boost::function classes which I thought might offer some kind of solution but they don’t seem to give me the right answer.

So, does anyone have any suggestions or work around on how to make this possible, if indeed it is? Personally I’m leaning towards having to template the various functions that require these objects – since that’s the functionality templates provide – but thought it worth investigating first.

Thanks in advance

  • 1 1 Answer
  • 1 View
  • 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-05-15T19:53:01+00:00Added an answer on May 15, 2026 at 7:53 pm

    What’s not entirely clear to me is how you’re resolving the parameter T to Func2, do you need some kind of dynamic dispatch on that too, or is it known at compile time at the call site?

    In the former case, it sounds like multimethods. In the latter, how about this variation on your interface idea:

    #include <iostream>
    
    template<class T> struct generic_delegate
    {
      virtual void call(T param) = 0;
    };
    
    template<class U, class T> class fn_delegate : public generic_delegate<T>
    {
      U* obj;
      void (U::*fn)(T);
    
    public:
      fn_delegate(U* o, void (U::*f)(T)) :
        obj(o), fn(f)
      {}
    
      virtual void call(T param)
      {
        (obj->*fn)(param);
      }
    };
    
    
    class A
    {
    public:
      template<class T> void fn(T param)
      {
        std::cout << "A: " << param << std::endl;
      }
    };
    
    class B
    {
    public:
      template<class T> void fn(T param)
      {
        std::cout << "B: " << param << std::endl;
      }
    };
    
    
    template<class T, class U> generic_delegate<T>* fn_deleg(U* o)
    {
      return new fn_delegate<U, T>(o, &U::template fn<T>);
    }
    
    
    int main()
    {
      A a;
      B b;
    
      generic_delegate<int>* i = fn_deleg<int>(&a);
      generic_delegate<int>* j = fn_deleg<int>(&b);
    
      i->call(4);
      j->call(5);
    }
    

    Obviously, the thing you’d be passing around are the generic delegate pointers.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a number of objects, all from the same class(ColorNum) Each object has
I'm testing a C++ class with a number of functions that all have basically
I have a class which is called a number of times. When the application
I have simple class with width and height member fields which define number of
I have this class that tries multiple methods of getting data from Google maps
In my class I have an assignment to create a Number class, which has
I have a simple custom NumberField: class NumberInput(forms.widgets.Input): input_type = 'number' class NumberField(forms.DecimalField): def
I have a number of classes that register with a notification class in my
I have a class that spawns an arbitrary number of worker object that compute
I have code: class Scene def initialize(number) @number = number end attr_reader :number end

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.