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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:20:59+00:00 2026-06-12T08:20:59+00:00

Let’s say i have following class: template <typename T> class CModule{ public: virtual void

  • 0

Let’s say i have following class:

template <typename T>
class CModule{
public:
  virtual void process( std::multiamp<int, T>)  = 0;
 }

and derived class:

template <typename T>
class CModuleDeriv: public CModule<T>{
public:
  virtual void process( std::multiamp<int, T>){....};

 }

and class where i wan’t to implement this functionality:

class Client{

std::vector<CModule<T>*> oModuleList_; // <--- this is not possible error

public:
  void moduleLoader(){
    oModuleList_.resize(1);
    if( some_condition ){
      oModuleList_[0] = CModuleDeriv<int>();
    }else{
      oModuleList_[0] = CModuleDeriv<double>();
    }
  }
}

is it possible?
is there any other solution ?
I can’t use boost :/

  • 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-12T08:21:01+00:00Added an answer on June 12, 2026 at 8:21 am

    First of all, in the future please post code that almost compiles (or even better one that compiles!), because those “little” details you omit sometimes changes the semantics of your code. So, submitting almost compilable code has almost unambiguous semantics…

    Here’s some code that might or might not solve your problem:

    #include<map>
    #include<vector>
    
    template <class, class>
    class TypedMultimap;
    
    class TypeErasedMultimap {
    public:
        template <class T1, class T2>
        std::multimap<T1,T2> & getMap() {
            return  static_cast<TypedMultimap<T1,T2> &> (*this); // This is not type safe, it can be made to be, but boost people already did it so I won't bother
        }
    
        virtual ~TypeErasedMultimap(){}
    };
    
    template <class T1, class T2>
    class TypedMultimap: public TypeErasedMultimap, public std::multimap<T1,T2> {};
    
    class CModule{
    
        virtual void process( TypeErasedMultimap &)  = 0;
    
    };
    
    template <typename T>
    class CModuleDeriv: public CModule{
    
        // At his point, please ask yourself, how will you make sure that the right kind
        // of map arrives at this call? I can't answer this, since this is related to
        // the semantics...
        virtual void process( TypeErasedMultimap & map_){
            std::multimap<int,T> &map = map_.getMap<int,T>();
            //...
        };
    
    };
    
    class Client{
    
    // Why are you using raw pointers?!?
    std::vector<CModule*> oModuleList_; 
    
    public:
      void moduleLoader(){
        oModuleList_.resize(1);
        if( 1/*some condition*/ ){
          oModuleList_[0] = new CModuleDeriv<int>();
        }else{
          oModuleList_[0] = new CModuleDeriv<double>(); // the types are lost forever...
        }
      }
    
    // ~Client() you MAY OR MAY NOT!!! need a destructor since your vector is holding pointers: use smart pointers!!!
    };
    
    int main() {
    }
    

    This, by itself is not a solution to your problem, because your not compiling code snipplet would not solve anything even if it compiled. You are pushing things with inherently different types to a common list and losing their type, so how will you know how to use the elements in the future? This is a semantic question.

    I’ll have a wild guess that this is probably what you’re trying to do:

    #include<boost/variant.hpp>
    #include<boost/shared_ptr.hpp>
    #include<boost/make_shared.hpp>
    #include<map>
    #include<vector>
    
    typedef boost::variant<std::multimap<int,int>, std::multimap<int,double> /* possibly some others */ > VariantMap;
    
    class CModule{
    public:
        virtual void process( VariantMap &)  = 0;
    
    };
    
    class CModuleDeriv1: public CModule, public boost::static_visitor<> {
    public:
        virtual void process( VariantMap & in){
        boost::apply_visitor(*this, in);    
        };
    
        template < class T>
        void operator()(std::multimap<int,T> & in) {
        // do your type safe processing here
        }
    };
    
    class CModuleDeriv2: public CModule, public boost::static_visitor<>{
    public:
        virtual void process( VariantMap & in){
        boost::apply_visitor(*this, in);
        };
    
        template < class T>
        void operator()(std::multimap<int,T> & in) {
        // do other kind of processing here
        }
    };
    
    
    class Client{
    
    // Why are you using raw pointers?!?
    std::vector<boost::shared_ptr<CModule> > oModuleList_; 
    
    public:
      void moduleLoader(){
        oModuleList_.resize(1);
        if( 1/*some condition*/ ){
          oModuleList_[0] = boost::make_shared<CModuleDeriv1>();
        }else{
          oModuleList_[0] = boost::make_shared<CModuleDeriv1>(); // the types are safe now, even though not known
        }
      }
    
    // ~Client() you MAY OR MAY NOT!!! need a destructor since your vector is holding pointers: use smart pointers!!!
    };
    
    int main() {
    }
    

    See, no more commented nags 🙂

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

Sidebar

Related Questions

Let's say I have the following classes : public class MyProductCode { private String
Let's say you have a class called Customer, which contains the following fields: UserName
Let's say I have the following function in C#: void ProcessResults() { using (FormProgress
Let me explain best with an example. Say you have node class that can
Let's say on a page I have alot of this repeated: <div class=entry> <h4>Magic:</h4>
Let's say I have the following text: (example) <table> <tr> <td> <span>col1</span> </td> <td>col2</td>
Let's say I have the following object: var VariableName = { firstProperty: 1, secondProperty:
Let's say I have a domain object with the following field: private Map<StatType, Double>
Let say I have the following desire, to simplify the IConvertible's to allow me
Let's say I have 2 functions: void function1(int *ptr) { printf(%d, *ptr); } and

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.