Hi,
For example:
Consider a Variant class ( which can hold a number of types like
long/int/string etc)
To save on memory you define a union with all those variables
Now to properly construct a string in the area of the union you would need
placement new
On destruction of your string (maybe because the variant type is assigned a
'long') you have to destruct the string, but not free its memory (it can't
since it is not allocated on the heap, but in the area where the union is
located ) in this case you would explicitely call delete:
Example:
union {
Int8 Char;
Int64 Long;
//UInt8 UChar;
//UInt64 ULong;
double Double;
bool Bool;
char String [ sizeof( std::string ) ];
char Map [ sizeof( std::map<UVar*,UVar*, UFindVar> ) ];
char SRefPtr[ sizeof( MSRefPtr<ISerialize> ) ];
char WRefPtr[ sizeof( MWRefPtr<ISerialize> ) ];
char KeyStroke[ sizeof( MKey ) ];
};
UVar::UVar( const string& String ):
Type ( eString )
{
new( this->String ) string( String );
}
void UVar::CleanUp()
{
switch( Type )
{
case eString:
reinterpret_cast<std::string *const>( String )->~string();
break;
//...etc
}
}
Regards, Ron AF Greve
http://moonlit.xs4all.nl