如何与非平凡的成员这样的工会像班级一样正常工作
How to make union like class with non-trivial member work properly?
我在试图与具有非平凡驱动器工作的成员进行类似工会的类别时面临两个问题。我无法理解的细分故障,也是一种有条件地调用非平凡驱动器的方法。
#include <iostream>
#include <string>
using namespace std;
class A {
struct B {
int u;
double v;
string w;
};
union C {
B x;
unsigned char y;
C(int u, double v, const string& w) {
x.w = w;// why does it segfault here?
x.u = u;
x.v = v;
}
C(unsigned char c) {
y = c;
}
//omitting this complains because string has non-trivial destructor
~C() {
// how to call this conditionally based on A::isChar value
//x.w.~string();
}
} c;
bool isChar;
public:
A(unsigned char f)
: isChar{true}, c(f) {}
A(int g, double h, const string& i)
: isChar{false}, c(g, h, i) {}
friend ostream& operator<<(ostream& os, const A& a) {
if(a.isChar)
return os << a.c.y;
return os << a.c.x.u << " " << a.c.x.v << " " << a.c.x.w;
}
};
int main() {
A a1(10, 20.0, "samik"), a2('v');
cout << a1 << " " << a2 << endl;
return 0;
}
我将使用g++ -g -std=c++14 -pedantic union_class_test.cpp -o union_class_test.out
与GCC 5.3.1一起编译。如何正确实施此操作?
上的程序segfaults
x.w = w;// why does it segfault here?
因为X尚未构造,并且X.W包含垃圾。您需要确保B的构造函数首先称为。
C(int u, double v, const string& w) : x()
{
x.w = w;// why does it segfault here?
x.u = u;
x.v = v;
}
为了正确破坏,您需要在A的驱动器中进行。离开C :: 〜C空,然后添加一个破坏者到a。
~A()
{
if (!isChar)
{
c.x.w.~string();
}
}
复制A时,您还需要小心,因为有时需要调用字符串的驱动器,有时不会被调用。这样做的一种方法是将新的位置用于覆盖this
,并且可以像...
A& operator=(const A& o)
{
if (this != &o)
{
this->~A();
if (o.isChar)
{
new (this) A(o.c.y);
}
else
{
new (this) A(o.c.x.u, o.c.x.v, o.c.x.w);
}
}
return *this;
}
和,移动将类似地实施。但是我将把它作为练习给读者。
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 像自定义类一样构造的指针(内置类型)如何工作?
- C++11: 如何编写一个像 Get 一样工作的模板函数<tuple>...但是收到参数包?
- 右值引用是否像右值引用一样工作
- C 参考与宏替换一样工作
- 为什么 gRPC C++ 客户端在没有显式服务器的 SSL 证书的情况下无法工作,就像在示例中一样?
- 如何与非平凡的成员这样的工会像班级一样正常工作
- 如何在 C++ 中创建一个类,使其像本机 int 类一样工作
- C++ 二叉搜索算法像lower_bound一样工作
- 是否有可能使自己的winapi功能像getpixel一样在Windows上工作
- 如何让 ss << f 像 printf( "%g" , f) 一样工作;
- 定义一个像 Python "with" 语句一样工作的 C 宏有什么缺点?
- c++中是否有任何数据结构或库可以像Python列表和字典一样工作?
- 为什么int8像char一样工作(c++) (Visual Studio 2015)
- 为什么getchar像缓冲区一样工作,而不是像预期的那样实时工作?
- 搜索一个数据结构,它可以像地图一样工作,也可以检索其他信息
- 传递常量引用…它像最重要的const一样工作吗
- 我能让赋值运算符像复制构造函数一样工作吗
- C++:"such strings"类型。使像 fun( "str" ) 一样的函数调用工作
- 我怎样才能让一对向量像哈希表一样工作呢?