The answer is no. There are many dark corners in C ++, but this is not one of them :)
classes and structures have inheritance. no unions.
The only way to accomplish what you are trying ... is reorganizing your unions into structures (I say that structs are only because they have a public domain by default, so you don't need to declare them public)
If you try to place a structure within your union, you will need to add an additional area of ββthe new structure in order to access its value.
Just as AndyG's answer shows this:
union TestFails { struct foo: public Base {}; foo f; int b; }; TestFails fails; fails.fa = 42; std::cout << fails.fa << std::endl;
If you omit the specified variable and create an unnamed scope that sometimes runs with namespaces, then data cannot be accessed from outside (which is the point of this in the first place).
Dan
source share