Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

unions c++

A union is a user-defined type in which all members share the same memory location. 
This definition means that at any given time, a union can contain no more than one object from its list of members. 
It also means that no matter how many members a union has, it always uses only enough memory to store the largest member.

A union can be useful for conserving memory when you have lots of objects and limited memory. 
However, a union requires extra care to use correctly. 
You're responsible for ensuring that you always access the same member you 
assigned. If any member types have a non-trivial constructor, then you must
write additional code to explicitly construct and destroy that member. 
Before you use a union, consider whether the problem
you're trying to solve could be better expressed by using a 
base class and derived class types.
 
PREVIOUS NEXT
Tagged: #unions
ADD COMMENT
Topic
Name
3+9 =