|
|
Darren New <dne### [at] sanrrcom> wrote:
> OK. In C#, the generic class "Nullable" takes a type argument.
> Declaring X to be of type Nullable<T> means that X has the methods
> X = (T) Y; // This lets you turn a T into a Nullable<T> in other words
> X.HasValue is a boolean that returns true.
> X = null; // This makes X.HasValue return false
> X.Value returns a value of type T if and only if X.HasValue is true.
What does X.Value return if X.HasValue is false?
> (You could probably do the same thing with multiple inheritance in C++, but
> C# doesn't really have that.)
I can think of two possible ways. Either multiple inheritance, like:
class NullableInteger: public Integer, public Nullable
{};
or using composition with a template class, like:
template<typename T>
class Nullable
{
T mValue;
bool mHasValue;
public:
Nullable(): mHasValue(false) {}
Nullable(const T& value): mValue(value), mHasValue(true) {}
void setNull(bool b) { mHasValue = !b; }
bool hasValue() const { return mHasValue; }
T& value() { return mValue; }
const T& value() const { return mValue; }
};
The latter could be used for example like:
Nullable<int> value = 5;
> > I can't comprehend what this has to do with I/O
> [...]
So it's all related to preserving the order in which I/O is performed?
--
- Warp
Post a reply to this message
|
|