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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:28:26+00:00 2026-06-04T07:28:26+00:00

I’m trying to make a component manager class in C++ that is similar to

  • 0

I’m trying to make a “component manager” class in C++ that is similar to the one found in Unity3d. What I want is a container class that can hold derived types of some class say BaseComponent. In unity, one can request for a component using generics in a type-safe way via

manager.GetComponent() which return type is DerivedComponentType&

I’d like the same interface in C++. This way, so long as components are added through a AddComponent function, the whole process is guaranteed to be type-safe from a usage standpoint.

I’m trying here to avoid things like identifying with strings. I realize I could also do this by giving every derived type a static member function of the same name and use the address of that as a map index to the class instances. I would rather not do this so that users of the component manager wont have to make sure this function exists should they ever choose to derive their own components.

Thanks.

  • 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-04T07:28:27+00:00Added an answer on June 4, 2026 at 7:28 am

    One way you can do it is with dynamic_cast.

    template <typename Derived>
    Derived* GetComponent (void)
    {
      // componentlist is a std::vector<BaseClass*>
      for (unsigned i = 0; i < componentlist.size(); ++i)
      {
        Derived* val = dynamic_cast<Derived*>(componentlist[i]);
        if (val != 0)
          return val;
      }
      return 0;
    }
    

    If the type doesn’t match what is given by the template, dynamic_cast will give a null pointer. Of course this is something that has to be done at runtime. On a modern machine this won’t be a huge hit unless you’re doing it way too often. There are a few other ways to do it that would be far more involved.

    You could also look into writing a reflection system that gives a faster way to find type information, so that you have something like this:

    Derived* GetComponent (MetaData *TypeIWant)
    {
      if (componentlist[i]->Type() == TypeIWant)
      //...
    }
    

    This would be far more work in the long run, and would require doing some research on your part, but it is perhaps the only c++ option I know of that doesn’t require the use of string lookup and the “dreaded” dynamic_cast so many people tend to hate.

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

Sidebar

Related Questions

I am doing a simple coin flipping experiment for class that involves flipping a
I have a French site that I want to parse, but am running into
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to create an if statement in PHP that prevents a single post
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.