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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T06:45:03+00:00 2026-06-12T06:45:03+00:00

I got to know about run-time type information in c++. This can be accomplished

  • 0

I got to know about run-time type information in c++. This can be accomplished with
typeid keyword in c++.

int main()
{
        //these two where user-defined class
        Complex x;
        Point3D y;

        const type_info& s = typeid(x);

        cout<<typeid(y).name()<<endl; //works...(1)
        cout<<s.name()<<endl;         //works...(2)

        Complex * tp = new s[10];     //does not work...(3)
}

As marked in the code, I was successful in printing out types of the data objects as in (1) and (2).

Now I wish to allocate memory by using type_info/typeid. Could anyone do this? Is it even possible. I do not have any virtual functions.

Can such a feat be accomplished by any other way. I do not want to use virtual functions as it has a negative effect on code vectorization.

  • 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-12T06:45:04+00:00Added an answer on June 12, 2026 at 6:45 am

    No, this is not possible. What you are looking for is a factory.

    Here is an example of such a factory. Suppose you have these types:

    class point_t
    {
    };
    
    class complex_t : public point_t
    {
    };
    
    class point3d_t : public point_t
    {
    };
    

    You can define the following factory:

    typedef point_t* (*makeptr)();
    
    class point_factory
    {
       static point_t* make_complex()
       {
          return new complex_t();
       }
    
       static point_t* make_point3d()
       {
          return new point3d_t();
       }
       static std::map<std::string, makeptr> _map;
    public:
       static void init()
       {
          _map.insert(make_pair(typeid(complex_t).name(), &point_factory::make_complex));
          _map.insert(make_pair(typeid(point3d_t).name(), &point_factory::make_point3d));
       }
       static point_t* make_point(const type_info& type)
       {
          std::map<std::string, makeptr>::iterator pos = _map.find(type.name());
          if(pos != _map.end())
             return (*(pos->second))();
          return NULL;
       }
    };
    
    std::map<std::string, makeptr> point_factory::_map;
    

    And use it like this:

    point_factory::init();
    point_t* p = point_factory::make_point(typeid(point3d_t));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know about Sonar but I'd rather not (actually, can't) run a whole web
I've got a problem where I need to know about all the bindings that
I have got a performance problem about TextField.htmlText +=msg .And I know that TextField.appendText(msg)
I know this is a frequently asked question and I havent got a clear
I don't know if I have got this correct but what I am trying
I've got a problem and I just don't know how to solve this (relatively)
I know I've had this problem before so I'm really frustrated. I've got the
So I've got this database that helps organize information for academic conferences, but we
I don't know where I've got a mistake, when I fire below code from
I've got magazine app and I want to know If is there any way

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.