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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T08:01:24+00:00 2026-06-04T08:01:24+00:00

I have a C++ class hierarchy defined by inheritance and I store a description

  • 0

I have a C++ class hierarchy defined by inheritance and I store a description of this hierarchy in it that I can later use for introspection. I would like to know if there is a more efficient or cleaner way to define this than the way I currently do it.
Here is a stripped down version of my code

// in header file (hpp)
struct Type
{
    Type( const string& n, const Type* p = nullptr ) : name(n), parent(p) {}
    const string name;
    const Type* parent;
};

class Base
{
public:
    static const Type m_type;
    virtual const Type& type() const { return m_type; } 
};

class Derived : public Base
{
public:
    static const Type m_type;
    const Type& type() const { return m_type; }
};

// in implementation file (cpp)
const Type Base::m_type( "Base" );
const Type Derived::m_type( "Derived", &Base::m_type );
  • 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-04T08:01:27+00:00Added an answer on June 4, 2026 at 8:01 am

    Not necessarily more efficient but think whether you actually want to require a common base class. An alternative approach uses a global type information registry. Then querying a type’s type information is done via TypeInfo::get(my_variable) or TypeInfo::get(typeid(my_type)).

    This has the advantage that it also works with existing types, which just need to be added to this type info registry.

    Internally, the registry would use a map from std::type_info to Type or similar. The following is a proof of concept. Unfortunately the code doesn’t compile on either clang or GCC. Based on the error messages, I’m suspecting a bug but I could also be wrong …

    struct Type {
        std::string name;
        std::vector<Type*> parents;
        // TODO Extend by fully-qualified name (namespace) etc.
    
        template <typename... T>
        Type(std::string&& name, T*... parents)
            : name(name), parents{parents...} { }
    };
    
    struct TypeInfo {
        template <typename T>
        static Type const& get(T const&) { return get(typeid(T)); }
    
        template <typename T>
        static Type const& get() { return get(typeid(T)); }
    
        static Type const& get(std::type_info const& info) {
            auto i = types.find(info);
            if (i == types.end())
                throw unknown_type_error(info.name());
    
            return i->second;
        }
    
        template <typename T>
        static void register_type(Type&& type) {
            types.insert(std::make_pair(typeid(T), type));
        }
    
        typedef std::unordered_map<std::type_info, Type> type_dir_t;
        static type_dir_t types;
    };
    

    Full code available as gist on github.

    Having a common base class for logically unrelated classes is generally frowned upon in C++, although it could be argued that this is similar to CRTP / mixins, in which common base classes are encouraged. So I’d say that there isn’t necessarily anything wrong with the approach if you don’t care for existing types.

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

Sidebar

Related Questions

I have a small class hierarchy where I would like to have a child
I have an hierarchy of classes that looks like the following: class Critical {
For example, I have some class hierarchy (possibly, with all kinds of inheritance -
I have a simple Class Hierarchy that I am trying to get to work
I have a quite complex class hierarchy in which the classes are cross-like depending
I have a hierarchy of application contexts. The bean that is defined in the
I have a small class hierarchy that I'm having trouble implementing copyWithZone: for. I've
I doubt this is possible but what I would like to do is have
I have a class hierarchy like so: (=> means is a subclass of) anonymous
Currently I have class hierarchy defined with the Code First approach as follows. E.F.

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.