In main
you have these two lines:
structExampleType<int>* structExamplePointer;
structExamplePointer->_structField=100;
Remember that local variables are not initialized unless they have a default constructor (which pointers do not have), so in the second you dereference the uninitialized pointer structExamplePointer
, leading to undefined behavior.
You also seem to misunderstand the purpose of union
, as you try to set multiple fields. In a union
all fields share the same space, so when writing to one member then all members are changed. Only the last written to member is valid. Trying to use other fields may again lead to undefined behavior.
If you need to have a "tag" field and other data, a structure containing the tag and the union data should be your choice:
struct NodeType
{
enum TAG_Type
{
FIELD1,
FIELD2
} type;
union
{
char field1;
struct some_struct* field2;
}
};