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

  • Home
  • SEARCH
  • 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 1019989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T11:05:57+00:00 2026-05-16T11:05:57+00:00

I need to write a function which could create any type object. It receives

  • 0

I need to write a function which could create any type object. It receives class name as parameter.
If classes are similar, we can derive all those classes from a single base class and let function return Base *. User of the function could use runtime polymorphism to use the returned object. In this case, function looks like below.

Base* createObject(string objName)
{
   if(objName == "D1")
       return new D1;
   else if(objName == "D2")
       return new D2;
   return NULL;
}

If the classes are dissimilar, they can’t be inherited from a single base class as it would not be proper inheritance. In this case, above function would not be useful.
Lets say i have 3 dissimilar classes like Helicopter, Kitchen and College.
In this case, how does single function could create any kind of object ?
I have one solution like below.
Use a wrapper class to wrap all dissimilar class pointers in an Union.
Then let function create Wrapper object and fill appropriate class pointer based on classname passed to it.
That functin looks like below

Wrapper* createObject(string objType)
{
     Wrapper *pWrapper = new Wrapper();
     pWrapper->objectType = objType;
     if(objType == "Helicopter")
     {
         pWrapper->pHelicopter = new Helicopter;
     }
     else if(objType == "Kitchen")
     {
         pWrapper->pKitchen = new Kitchen;
     }
     else if(objType == "College")
     {
         pWrapper->pCollege = new College();
     }
     return pWrapper;
}

Wrapper class looks like below.

class Wrapper
{
    public:
    string objectType;
    union
    {
        Helicopter *pHelicopter;
        Kitchen *pKitchen;
        College *pCollege;
    };
};

Is there any better solution than above ?

  • 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-16T11:05:57+00:00Added an answer on May 16, 2026 at 11:05 am

    Boost already has 2 possible solutions to your problem: Boost.Variant and Boost.Any.

    But preferably you should avoid such a design in the first place if you can. If one function returns unrelated objects and you can’t refactor to give the types a common interface, you should probably split it into multiple functions.

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

Sidebar

Related Questions

I need to write a TSQL user defined function which will accept a string
I need some help in getting this right, problem Write a function which takes
I need to write a function that can take an if statement at runtime
I need to write a function that is passed an instance of a CharField.
I am in need to write a function in postgeresql/postgis to update m-value in
I need to write a delegate function that can 'wrap' some while/try/catch code around
I need to concatenate the parameters in the write function. How would I do
I need to write a little ruby function that does word wrapping. I have
I need to write a non-recursive version of the function sum-squares and Use a
Basically what I need to do is write a function that takes in a

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.