jweinst1
8/26/2017 - 6:59 PM

unionobjconstructors.cpp

#include <iostream>

struct RadonObject
{
	enum
	{
		Int,
		Char,
		Bool,
		List,
		Rx
	} state;

	union
	{
		RadonObject* _list;
		int _int;
		char _char;
		bool _bool;
	};

	RadonObject* next;

	RadonObject(int i, RadonObject* next = nullptr): state(RadonObject::Int), _int(i), next(next) {}
	
	RadonObject(char ch, RadonObject* next = nullptr): state(RadonObject::Char), _char(ch), next(next) {}
	
	RadonObject(bool b, RadonObject* next = nullptr): state(RadonObject::Bool), _bool(b), next(next) {}
	
	RadonObject(RadonObject* lst, RadonObject* next = nullptr): state(RadonObject::List), _list(lst), next(next) {}
};

int main() {
  RadonObject* rad = new RadonObject(3, 
  new RadonObject('f' , 
  new RadonObject(new RadonObject(4), new RadonObject(5))));
  std::cout << rad->_int << "\n";
  std::cout << rad->next->_char << "\n";
  
}