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);
private:
TargetType targetType;
union
{
FVector location;
TWeakObjectPtr<AActor> targetActor;
};
};
Source:
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)
{
}
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
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
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?