假设我有一个C++类这样的:如何初始化指向nullptr的指针联合?
class Container
{
private:
union {
Foo* foo;
Bar* bar;
} mPtr;
};
这个类将在堆栈上构成。即它不会被new
编辑,所以我不能只声明一个归零operator new
。
我可以使用构造函数中的初始值设定器来设置mPtr
到nullptr
莫名其妙吗?
Container()
: mPtr(nullptr)
{
}
不能编译。甚至没有添加nullptr_t
类型的虚拟union
成员。
Container()
: mPtr.foo(nullptr)
{
}
也不编译。
尝试':mPtr(NULL)',您还可以定义联合的构造函数。为此,它必须通过AFAIK命名 – Paranaix