a.) Let's assume you have a template class something like
template <class T>
class TNullable
{
}
b.) Then it is a good practice to define a type to use the template class
typedef TNullable<int> TNullableInt;
c.) Use the template class
TNullableInt myNullableIntVariable;
Note: Of course you can make it shorter and use directly
TNullable<int> myDirectNullableIntVariable;
I hope it is helpfull and not too abstract ;)
To give you an idea, a possible implementation of TNullable
template <class T>
class TNullable
{
private:
bool FIsNull;
T FValue;
const T& GetValue() const
{
if (FIsNull)
{
throw(*new Exception("TNullable Null Value Access"));
}
return(FValue);
}
void SetValue(const T& Value)
{
FValue= Value;
FIsNull= false;
}
public:
TNullable()
: FIsNull (true),
FValue ()
{
}
TNullable(const T& rhs)
: FIsNull (false),
FValue (rhs)
{
}
virtual ~TNullable(){}
TNullable (const TNullable& rhs)
{
operator=(rhs);
}
const TNullable& operator=(const TNullable& rhs)
{
if (this != &rhs)
{
FIsNull = rhs.FIsNull;
FValue = rhs.FValue;
}
return(*this);
}
const T& operator=(const T& rhs)
{
FIsNull = false;
FValue = rhs;
}
bool operator== (const TNullable& rhs) const
{
if (FIsNull != rhs.FIsNull)
{
return(false);
}
else if (FIsNull)
{
return(true);
}
else
{
return(FValue == rhs.FValue);
}
}
bool operator!= (const TNullable& rhs) const
{
return(!operator==(rhs));
}
__property bool IsNull = {read= FIsNull, write= FIsNull};
__property T Value = {read= GetValue, write= SetValue};
};