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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:44:40+00:00 2026-06-17T19:44:40+00:00

I have a class containing union as a field. The union is of pointers

  • 0

I have a class containing union as a field. The union is of pointers to two different classes. As a second field my class contains a flag informing which type is currently stored.

class Item {
    std::string *title;
    bool who_am_I;
    union { Submenu *smenu; Function *call; } content;
    public:
    bool am_I_a_submenu();
    bool am_I_a_function();
    Submenu *give_me_submenu();
    Function *give_me_function();
    /*(...)*/
};

Now, before each usage of my “give_me” methods I urge user to check the type through the appropriate method accessing the flag, i.e. the “am_I” methods. Nevertheless I would like my library to throw appropriate exception if the user would happen to forget about it. Can I do that without checking the flag inside the “give_me” method? I ask because it would mean that in normal usage the flag is unnecessarily checked twice.

I was wondering if, which and when does c++ throw some in build exception once the conflict of types will appear causing program malfunctioning. Or maybe should I handle this case otherwise, still preferably without double checking the flag.

  • 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-17T19:44:42+00:00Added an answer on June 17, 2026 at 7:44 pm

    OK, first of all… why do you care if the flag is checked twice? Is that really a serious performance bottleneck in your product that profiling has shown must be optimized? I very much doubt it.

    But even if it is a performance bottleneck, what’s more important? A properly functioning application that doesn’t crash seems to me to be an acceptable tradeoff for a super-tiny amount of extra overhead.

    But you should probably just redesign your interface(s) so that the user always knows what he has and can’t make mistakes like this.

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

Sidebar

Related Questions

I have a class A containing two pointers to objects of another class B.
I have an example class containing two data points: public enum Sort { First,
I have a container class (containing a multi-index container) for which I have a
I have designed a class containing some information about a given object which will
I have a class library containing a number of classes, and in each class
I have a class containing a set of attributes, which I want to be
I have a class containing two properties: public class player{ public player(String playerName,int points){
I have a class Foo, which has a (simple) destructor. Some other class contains
I have two divs one is both are containing one same class and all
I have class containing an std::set of boost::weak_ptr<T> . I have two functions begin()

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.