简体   繁体   中英

How are member unions containing primitives and objects initialized in C++?

Say I have:

class Foo {
  union {
     double a;
     std::string b;
  };
};

What will the default constructor generated by the compiler do? My understanding of the C++ standard is that primitives are not initialized but objects are. So what happens here?

The default constructor (as well as the copy constructor, assignment operator and destructor) for the union is implicitly declared deleted, and so is the constructor of Foo . You'll see this once you try to actually create an instance of Foo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM