遇到“联合”问题强迫所有成员,不仅是积极的成员。 [英] Having problems with "union" forcing initiation of all members, not only active one.

查看:92
本文介绍了遇到“联合”问题强迫所有成员,不仅是积极的成员。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨!



我有以下代码(删除了一些不必要的细节):



标题:

Hi!

I have the following code (with some unnecessary details removed):

Header:

enum class TargetType : uint8
{
	NoTarget
	, Location
	, Actor
};


class Target
{
public:

	Target();
	Target(const FVector &inLocation);
	Target(const AActor *inTargetActor);

	// ... some methods here

private:

	TargetType targetType;

	union
	{
		FVector location;
		TWeakObjectPtr<AActor> targetActor;
	};
};





资料来源:



Source:

// ... header includes

Target::Target()
	: targetType { TargetType::NoTarget }
{
}

Target::Target(const FVector &inLocation)
	: targetType { TargetType::Location }
	, location(inLocation)
{
}

Target::Target(const AActor *inTargetActor)
	: targetType { TargetType::Actor }
	, targetActor(inTargetActor)
{
}

// ... some code





当我尝试编译时,我收到以下错误:



When I try to compile this I get the following errors:

error C4582: 'Target::location': constructor is not implicitly called
error C4582: 'Target::targetActor': constructor is not implicitly called
error C4582: 'Target::targetActor': constructor is not implicitly called
error C4582: 'Target::location': constructor is not implicitly called





编译器抱怨工会成员没有被启动。第一个构造函数抱怨两个成员都没有被启动。另外两个人抱怨该成员没有被发起,所以例如如果我想使用该位置,编译器也希望我启动targetActor。



我的问题是,如果我是真的我必须启动两个成员不会使用它们,例如在第一个构造函数?对我来说没有意义,因为在这种情况下我不想使用数据?当我想使用



The compiler complains about the union members not being initiated. The first constructor complains about both members not being initiated. The other two complains about the member not being initiated, so e.g. if I want to use the location, the compiler wants me to initiate the targetActor as well.

My question is do I really have to initiate both members if I'm not going to use them, e.g. in the first constructor? For me it makes no sense since I don't want to use the data in that case? Also why do I have to initiate

Target::location

作为活跃成员时,为什么我必须启动

when I want to use

Target::targetActor

?我在这里错过了什么?



提前致谢!



我有什么尝试过:



我确定有人比我更了解发生的事情!

感谢所有帮助!



注意:我使用C ++ 11但只存在C ++ 14标签?

as the active member? I'm I missing something here?

Thanks in advance!

What I have tried:

I'm sure someone understands better than me what's going on!
Thankful for all help!

Note: I use C++11 but only the C++14 tag existed?

推荐答案

其中编译器版本(我猜微软)?

我用GNU编译器测试了类似的代码(使用其他联合成员类型)并且没有错误。



根据编译器的C ++ 11支持,可能源于联盟成员不是POD数据类型:

Which compiler version (I guess Microsoft)?
I have tested similar code (using other union member types) with the GNU compiler and got no errors.

Depending on the C++11 support of the compiler it might be sourced be the union members being not POD data types:

联合不能包含具有非平凡特殊成员的非静态数据成员function(复制构造函数,复制赋值运算符或析构函数)。 (直到C ++ 11)





如果一个联合包含一个带有非平凡的复制/移动构造函数的非静态数据成员,复制/移动赋值或析构函数),该函数在联合中默认删除,需要由程序员明确定义。



如果一个联合包含一个具有非平凡默认构造函数的非静态数据成员,默认情况下会删除union的默认构造函数,除非union的variant成员具有默认成员初始值设定项。



最多一个变体成员可以拥有默认成员初始值设定项。 (自C ++ 11起)

Unions cannot contain a non-static data member with a non-trivial special member function (copy constructor, copy-assignment operator, or destructor). (until C++11)


If a union contains a non-static data member with a non-trivial copy/move constructor, copy/move assignment, or destructor), that function is deleted by default in the union and needs to be defined explicitly by the programmer.

If a union contains a non-static data member with a non-trivial default constructor, the default constructor of the union is deleted by default unless a variant member of the union has a default member initializer.

At most one variant member can have a default member initializer. (since C++11)



因此可以通过提供合适的构造函数(需要使用非匿名联合)来解决:


So it might be solved by providing appropiate constructors (which requires using a non anonymous union):

union loc
{
    FVector location;
    TWeakObjectPtr<AActor> targetActor;
    loc() { /* initialise one member here */ }
    loc (const FVector &inLocation) { location = inLocation; }
    loc (const AActor *inTargetActor) { targetActor = inTargetActor; }
};


您需要为联合提供一个名称来访问该类中的成员。请参阅联盟 [ ^ ]。
You need to give the union a name to access its members within the class. See Unions[^].


这篇关于遇到“联合”问题强迫所有成员,不仅是积极的成员。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆