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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T03:17:37+00:00 2026-05-28T03:17:37+00:00

I have the following program: #include <initializer_list> #include <iostream> class A; class nih {

  • 0

I have the following program:

#include <initializer_list>
#include <iostream>

class A;
class nih {
public:
    virtual void apply(A & a) const { std::cout << "generic" << std::endl; };
};

union niit;
class A {
public:
  int s; char kau;
  A() : s(1), kau('-') { }
  A(std::initializer_list<niit> k);
};

class sn : public nih { int s; public: sn(int x) : s(x) { }
  virtual void apply(A & a) const { a.s=s; } };
class kaun : public nih { char kau; public: kaun(char x) : kau(x) { }
  virtual void apply(A & a) const { a.kau=kau; } };
union niit { sn sni; kaun kauni; nih nihi;
  niit(const sn & s) : sni(s) { }
  niit(const kaun & k) : kauni(k) { }
};

A::A(std::initializer_list<niit> k) { 
  const niit *n=k.begin();
  for(int i=0;i<k.size();i++) {
    const nih* p=&n[i].nihi;
    p->apply(*this);
  }
}

int main(int argc, char**argv) {
  A r {kaun('a'),sn(91)};
  std::cout << r.s << r.kau << std::endl;
}

I get the expected result, which is 91a. However, if I change the line

p->apply(*this);

to the following:

((const nih*)(&n[i].nihi))->apply(*this);

which should be replacement with an equivalent value, it no longer works, and instead it prints:

generic
generic
0

That is, it executes the generic method from nih. Why is that? I tried gcc 4.6 with option -std=c++0x, in case it matters.

  • 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-28T03:17:37+00:00Added an answer on May 28, 2026 at 3:17 am

    A union can only be used with POD types, and as such cannot have members with virtual functions or nontrivial constructors/destructors, so this is undefined behaviour. Read (taken from this answer) §9.5.1 of the standard:

    A union can have member functions (including constructors and
    destructors), but not virtual functions. A union shall not have base
    classes. A union shall not be used as a base class. An object of a
    class with a non-trivial constructor, a non-trivial copy-constructor,
    a non-trivial destructor, or a non-trivial copy assignment operator
    cannot be a member of a union, nor can an array of such objects. If a
    union contains a static data member, or a member of a reference type,
    the program is ill-formed.

    And

    In certain contexts, C++ allows only POD types to be used. For
    example, a union in C++ cannot contain a class that has virtual
    functions, or nontrivial constructors or destructors. This restriction
    is imposed because the compiler cannot know which constructor or
    destructor should be called for a union.

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

Sidebar

Related Questions

If I have the following basic C++ program: #include <iostream> using namespace std; class
I have the following test program. #include <iostream> #include <cstdlib> using namespace std; pthread_mutex_t
I have the following program test.cc: #include <iostream> unsigned char bogus1[] = { //
I have the following program: #include <iostream> struct X { int a; float b;
On Visual Studio 2010 the following program #include <iostream> using std::cout; int main() {
i have the following program. #include <iostream> #include <vector> #include <algorithm> #include <cassert> #include
I have the following program: class Matcher include Enumerable def initialize(string, match) @string =
Say I have the following program #include <stdio.h> #include <stdlib.h> int main(void) { int
i have following program to calculate size of file #include <iostream> #include <fstream> #include
I have the following c program which launches a Gtk Program on ubuntu: #include

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.