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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T11:25:38+00:00 2026-06-10T11:25:38+00:00

I have a certain problem and I was stuck at which approach is better

  • 0

I have a certain problem and I was stuck at which approach is better than the other:

A structure with an enum that defines the data in union member OR
A group of classes with inheritance

Sample code is given below:

Union based Structure

  typedef union TokenValue{
   bool bValue;
   long lvalue;
   double dvalue;
   std::string svalue;
   }

  class Token{
  public:
   TokenType type;
   TokenValue value;
   };

Class Inheritance

class TokenBase{
public:
  TokenType type;
  };



class TokenNumber: public TokenBase{
public:
bool isInt;
long lvalue;
double dvalue;
};

class TokenString: public TokenBase{
 public:
  std::string svalue;
 };
  • 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-10T11:25:40+00:00Added an answer on June 10, 2026 at 11:25 am

    This type of design is known as a discriminated union or tagged union. You can Google for “C++ discriminated union” to see several examples of and approaches to this type of design.

    A C union is usually considered a bit low-level and unsafe for C++. As Pete Becker said, your union-based solution should provide accessors instead of directly exposing the unsafe union member. unions also have the disadvantage that, prior to C++11, they can’t contain non-POD data structures (so no std::string).

    An inheritance-based solution has the advantage that you can start using polymorphism (e.g., add a virtual PrintTo method). (You can, of course, accomplish similar results by adding a PrintTo method to a union-based solution that does a switch over TokenType, but that’s non-OO.)

    A union-based solution would probably be lighter-weight than an inheritance-based solution (no vtable). That might be preferable for something as low-level as lexer tokens.

    If third-party libraries are permitted, I’d strongly recommend taking a look at Boost.Variant for a more C++ approach to discriminated unions.

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

Sidebar

Related Questions

I have a problem finding elements in XPath that's contains a certain string ignoring
I have a webpage that plays a 'chime' sound when certain events occur. Problem
Here's a problem. Developers have some kind of a task (develop certain feature), which
I am stuck on a weird problem. I have a CashGameGeneralViewModel class which looks
I have a problem positioning an element in certain browsers. I'm using the jQuery
i have a problem to order a list by certain rule. Actually i have
I have a problem in mysql. I need to fetch certain sets of records
I have a strange problem in my PhoneGap-based android app. On certain screens, the
I have been working on the following problem from this book . A certain
I have certain PHP class methods that access external variables. These variables are not

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.