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

  • SEARCH
  • Home
  • 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 9182073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:28:28+00:00 2026-06-17T18:28:28+00:00

I would like to create a class that acts as an interface to a

  • 0

I would like to create a class that acts as an interface to a set of specialized template classes. For example:

template<typename T>
class ThingDoer {
    public:
        void Method()
        {
            // do something;
        }
};

class ThingDoerInterface {
    public:
        template<typename T>
        void Method()
        {
            // call ThingDoer<T>::Method(); somehow
        }
};

int main()
{
    ThingDoerInterface i;
    i.Method<int>();
    i.Method<char>();
    // etc.
    
    return 0;
}

My generic requirements for the object I’d like look something like this:

  • A user needs to only create one, non-templated instance of the object.
  • But multiple instances can exist, and are expected to be independent.
  • The object associates an instance of an (user-defined) object derived from type A with (one or more) ones derived from type B.
  • A user can call a method on the object that does something with B based on the type of A.

I have a working solution to my actual problem that’s based on std::unordered_multimap, but I’m interested if something like this can be done with templates alone.

Edit:

This is a more specific example that I hope will illustrate what I am actually trying to do.

class ABase {
    public:
        virtual ~ABase() {}
};

class A1 : public ABase {};
class A2 : public ABase {};

class BBase {
    public:
        virtual ~BBase() {}
};
class B1 : public BBase {};
class B2 : public BBase {};


class ThingDoerInterface {
    public:
        template<typename T>
        void Store(BBase* b_ptr)
        {
            // store the B pointer with the type of T as a key
            // (T will be A1 or A2)
        }
        
        template<typename T>
        void Recall()
        {
            // call all the stored B pointers associated with the type of T
        }
};

int main()
{
    ThingDoerInterface i;
    
    B1* b_one_ptr = new B1;
    B2* b_two_ptr = new B2;
    
    i.Store<A1>(b_one_ptr);
    i.Store<A1>(b_two_ptr);
    
    i.Store<A2>(b_one_ptr);
    
    i.Recall<A1>(); // do something with b_one_ptr and b_two_ptr
    i.Recall<A2>(); // do something with b_one_ptr
    
    delete b_two_ptr;
    delete b_one_ptr;
    
    return 0;
}

And I have done this with an std::unordered_multimap, but what I want to know is if it is possible to store the association like this:

template<typename T>
class ThingDoer {
    public:
        void Store(BBase* b_ptr)
        {
            b_ptrs.push_back(b_ptr);
        }
        
        void Recall()
        {
            // do something with the b_ptrs associated with the type of T
        }
    private:
        std::vector<BBase*> b_ptrs;
};

but do so in the ThingDoerInterface somehow.

  • 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-17T18:28:29+00:00Added an answer on June 17, 2026 at 6:28 pm

    I’m not necessarily saying it’s worth the complexity, but…

    #include<iostream>
    #include<vector>
    using namespace std;
    
    class repo {
    public:
      repo(){
        id = highestid++;
      }
      template<typename T>
      T& get() {
        vector<T>& vec = data<T>::vec;
        if (vec.size() <= id) {
          vec.resize(id+1);
        }
        return vec[id];
      }
    private:
      template<typename T>
      struct data {
        static vector<T> vec;
      };
      int id;
      static int highestid;
    };
    /*static*/ int repo::highestid = 0;
    template<typename T>
    /*static*/ vector<T> repo::data<T>::vec;
    
    main(){
      repo a, b;
      a.get<int>() = 42;
      b.get<int>() = 54;
      a.get<float>() = 4.2;
      b.get<float>() = 5.4;
      cout << a.get<int>() << ", " << b.get<int>() << ", " << a.get<float>() << ", " << b.get<float>() << endl;
    }
    

    I’ve written it for fast lookup. The first time you call get for a given repo, you invalidate all references returned by other repos, and destroying a repo doesn’t clean up anything. But if you have a small number of repos whose lifetime is basically the whole program, that should be ok.

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

Sidebar

Related Questions

I have a class that I would like to create a simple example on
I would like to create a new class that acts as a special type
I would like to create a class that runs something (a runnable) at regular
I would like to create a class that behaves as a python variable but
motivation: I would like to create a utility class so that instead of having
I would like to create a class that creates and manages log files. I
I would like to create a control class that would allow the user to
I would like to create a class that effectively does this (mixing a little
I would like to create a singleton class that is instantiated once in each
I would like to create a LoadingDialog class that could be used by ANY

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.