Can a union in C++ have a member function? How do union with data members and member functions exist if an object is created?
If I suppose yes, then are they feasible an
You can also make a template union :
template
union Foo {
public:
Foo() {}
Foo(const T& value) : _val(value) {}
const char* data() const {
return _tab;
}
std::size_t size() const {
return sizeof(T);
}
char operator[](unsigned int index) const {
return _tab[index];
}
private:
T _val;
char _tab[sizeof(T)];
}