This is the relationship I am talking about:
struct A{
int i = 1;
};
struct B{
union{A a;};
};
void main(){
B b;
};
In this constellation, my compiler (vs2015) complains about the default constructor of B B::B(void)
beeing deleted, with the note that the compiler has generated B::B
:
../test.cpp(155): error C2280: "B::B(void)" : Es wurde versucht, auf eine gelöschte Funktion zu verweisen
../test.cpp(152): note: Compiler hat hier "B::B" generiert
(sorry, I could not convince msvc to talk english to me)
Either of these two code changes fixes it:
struct A{
int i; //removed initialzation of member to 1
};
or
struct B{
B(){} //adding explicit default constructor
union{A a;};
};
I know that adding a default constructor that does nothing is not exactly a complicated or annoying workaround, but I really want to know why C++ forces me to do this.