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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:45:05+00:00 2026-05-16T17:45:05+00:00

i have code like below. Base is the base class and D1, D2, D3

  • 0

i have code like below.
Base is the base class and D1, D2, D3 are derived classes.
D1, D2 and D3 class objects can hold int, float and double values respectively.
I have a vector of base class pointers. Each one of them can point to any of the derived class objects.
Through Base class pointer i should be able to get the data present in any of the derived class.

enum Type
{
   INT,
   FLOAT,
   DOUBLE
};
struct Data
{
   Type type;
   union
   {
       int iVal;
       float fVal;
       double dVal;
   };
};
class Base
{
public:
   virtual Data getData() = 0;
};
class D1: public Base
{
   int i;
public:
   virtual Data getData()
   {
       Data data;
       data.type = INT;
       data.iVal = i;
       return data;
   }
};

class D2: public Base
{
     float f;
public:
    virtual Data getData()
    {
       Data data;
       data.type = FLOAT;
       data.fVal = f;
       return data;
    }
};

class D3: public Base
{
     double d;
public:
    virtual Data getData()
    {
       Data data;
       data.type = DOUBLE;
       data.dVal = d;
       return data;
    }
};

getData() returns Data object.

Using the type in Data object, i need to extract corresponding value in union.

I am unable to avoid type checking even though i am using virtual function mechanism.

Is there any better way to return data through single interface function ?

EDIT: In my project scenario, at runtime it can generate few D1 objects, few D2 objects and so on. I need to pass all these objects to some other module.
So i used vector<Base *> to pass on all the objects to the next module.
Is there any better way to pass on all the objects ?

EDIT: Any Non-Boost solution exists ?

  • 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-16T17:45:05+00:00Added an answer on May 16, 2026 at 5:45 pm

    In general I’d prefer to avoid such design at all. But if you absolutely have to make it this way, the easiest and fastest method, imho, is to use boost::any.

    Update: As it was, absolutely correctly, noted boost::variant may be even more convenient and efficient here, since the used types are known (thanks for the remarks).

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

Sidebar

Related Questions

As you can see in the code below, I have an Abstract Base Class
I have a code like this below in /root_project/main.cpp : #include theoraplayer/TheoraVideoClip.h unsigned int
I have something like the code below: for(int i=0;i<10;i++){ button=new JButton(buttons[i]); button.addActionListener(new ActionListener(){ public
I have the Global.asax like the code below: public class MvcApplication : System.Web.HttpApplication {
I have code like this: class Base { public: void operator = (const Base&
I have a standard code like below to validate xml against xsd, but it
I have a code like this below, which gives me a $link that equals
I have code that looks more or less like the code below but it
Hi I have below pseudo code with throws an exception like this throw new
I have made a YUI module a little like the code shown below, which

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.