Visual Studio 中有没有一种方法可以处理非平凡的联合。以下代码运行良好g++ -std=c++11
但 VS 抱怨:
无效的联合成员 - 类“Foo”具有不允许的成员函数
代码如下:
struct Foo {
int value;
Foo(int inV = 0) : value(inV) {}
};
union CustomUnion {
CustomUnion(Foo inF) : foo(inF) {}
CustomUnion(int inB) : bar(inB) {}
int bar;
Foo foo;
};
int main() {
CustomUnion u(3);
return 0;
}
Visual Studio 中有没有办法支持这种联合(例如编译选项)?或者我应该更改我的代码,如果是的话,该怎么办?
我同意@Shafik Yaghmour 的观点,但有一个简单的解决方法。
只要把你的foo
会员成为unnamed struct
像这样:
union CustomUnion {
struct{
Foo foo;
};
int bar;
};
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)