Copying (using assignment) a structure to a structure inside a union causing seg fault

后端 未结 3 880
醉酒成梦
醉酒成梦 2020-12-22 03:22

I wrote the following code:

#include 
#include 
#include 

struct bar
{
  std::string s3;
  std::string s4;
}Ba         


        
3条回答
  •  刺人心
    刺人心 (楼主)
    2020-12-22 03:54

    union foo can't initialize the bar object (how does it know which member's initializer to call?) and consequently can't initialize the std::strings. If you want to use the bar inside of foo, then you need to manually initialize it, like so...

    new (&f1.b1) bar; // Placement new
    f1.b1 = b2;
    // And later in code you'll have to manually destruct the bar, because
    //   foo doesn't know to destruct the bar either...
    f1.b1.~bar();
    

    Alternatively, you can try to roll this functionality into the union's constructors and destructors yourself.

    foo() : b1() {}
    // Or you construct like this, which you might need to for a non-trivial union...
    // foo() { new (&b1) bar; }  // Placement new.
    ~foo() { b1.~bar(); }
    

    Note that copying also needs special handling.

提交回复
热议问题