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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T22:43:55+00:00 2026-05-31T22:43:55+00:00

I have class Base and classes Derived_1 , Derived_2 … I need derived classes

  • 0

I have class Base and classes Derived_1, Derived_2 …
I need derived classes to have an id. Those ids are used for further lookups etc, and thus need to be consecutive (no just some random numbers). Because derived classes are created by user, id can not be member of Derived_N. So I came up with DerivedType class.

class DerivedType
{
    static unsigned id;
    unsigned m_id;
public:
    DerivedType() : m_id(id++) {  }
}

Now I want to create a mapping between Derived_N and DerivedType.
Whenever Derived_N is created, this mapping looks if DerivedType for particular Derived_N already exist and returns it, otherwise create new and stores in the map.

Actual question:
Is there any way to use std::map with data type as key in the map?
I am not afraid of any template-metaprogram solution.
Or is there elegant way how to achieve my goal?

edit Date type -> Data type, I mean like ClassType, I am sorry 🙂

I want to use it like:

Derived_5 d;
DerivedType dt = getType(d); //Derived_5 is looked up in map, returning particular DerivedType
dt.getId();

every instance of Derived_N (with same ‘N’) should have the same id throu DerivedType

EDIT2 – MY ANSWER
I found better solution for my problem… It is like this:

atomic_counter s_nextEventClassID;

typedef int cid_t;

template<class EventClass>
class EventClassID
{
public:
    static cid_t getID()
    {
        static cid_t classID = EventClassID::next();
        return classID;
    }

    static cid_t next() { return ++s_nextEventClassID; }
};

since my question was how to use datatype in a map, I will mark some of yours answers, thank you

  • 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-05-31T22:43:56+00:00Added an answer on May 31, 2026 at 10:43 pm

    C++11 solves this by providing std::type_index, in <typeindex>, which is a copyable, comparable and hashable object constructed from a std::type_info object that can be used as the key in associative containers.

    (The implementation is fairly simple, so even if you don’t have C++11 yourself, you could steal the implementation from, say GCC 4.7, and use that in your own code.)

    #include <typeindex>
    #include <typeinfo>
    #include <unordered_map>
    
    typedef std::unordered_map<std::type_index, int> tmap;
    
    int main()
    {
        tmap m;
        m[typeid(main)] = 12;
        m[typeid(tmap)] = 15;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have base class BaseClass and derived classes DerivedA , DerivedB , and DerivedC
I have these classes: class Base { ... private: std::vector<X> v; }; class Derived
Let's say I have a Base class and several Derived classes. Is there any
I have a abstract base class A and a set of 10 derived classes.
I have a base class vehicle and some children classes like car, motorbike etc..
I have a base class called BaseEvent . And, 3 classes derived from BaseEvent
I have a base class and a dozen derived classes. All but one derived
I have a base class called Component which has a number of classes derived
I have a base class called Mijloc_transport and 2 derived classes called Masina and
I have a base class Rules.cs . There are 2 derived classes RowRules.cs 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.