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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:45:48+00:00 2026-05-30T18:45:48+00:00

I came across a strange phenomena upon running the following code: #include <iostream> class

  • 0

I came across a strange phenomena upon running the following code:

#include <iostream>    

class Piece {
public:
    class Queen;
    class Knight;
    union Any;
    virtual const char* name() const = 0;
};

class Piece::Queen : public Piece {
public:
    virtual const char* name() const {
        return "Queen";
    }
};

class Piece::Knight : public Piece {
public:
    virtual const char* name() const {
        return "Knight";
    }
};

union Piece::Any {
public:
    Any() {}
    Piece::Queen queen;
    Piece::Knight knight;
};

using namespace std;
int main(int argc, const char* argv[]) {
    Piece::Any any;
    any.queen = Piece::Queen();
    cout << any.queen.name() << endl;

    return 0;
}

The program compiled successfully on the Apple LLVM 3.0 compiler, but the output was “Knight”.
I was expecting for the output to be “Queen”.
From my testing I saw that when Piece::Any’s default constructor runs, it calls both Piece::Queen and Piece::Knights’ constructors, one after another. If I were to declare Piece::Any like this:

union Piece::Any {
public:
    Any() {}
    Piece::Knight knight;
    Piece::Queen queen;
};

(I basically swapped the order of knight and queen) then the output would be Queen.
Any help would be appreciated.

Thanks

  • 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-30T18:45:49+00:00Added an answer on May 30, 2026 at 6:45 pm

    First of all – your constructor seems to initialize none of its members. You should choose one, for example

    Piece::Any::Any(): knight() {}
    

    Then according to 9.5.4

    In general, one must use explicit destructor calls and placement new operators to change the active member of a union

    so correct switching from knight to queen is

    any.knight.~Knight();
    new(&any.queen) Queen;
    

    If it looks ugly to you (as it does to me), it is clear indication, that keeping objects with non-trivial constructors in union is not a good idea (how about boost::variant?).

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

Sidebar

Related Questions

I came across this strange code snippet which compiles fine: class Car { public:
I came across strange behavior in Visual Studio 2010 C++ compiler. Following code compiles
I came across on a strange behavior of the following code, while playing around
I came across some Java code that has a method containing the following: static
I am debugging some 3rd party code in IntelliJ 11 and came across following
While refactoring some code, I came across this strange compile error: The constructor call
Whilst reading through the DirectWrite source code I came across the following struct: ///
I came across some strange results when using Code Coverage for our unit tests.
I came across a rather strange problem with linq-to-sql. In the following example, var
I came across this strange looking declaration in some code early this morning (before

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.