I'm creating a database access layer in native C++, and I'm looking at ways to support NULL values. Here is what I have so far:
class CNullValue
{
public:
static CNullValue Null()
{
static CNullValue nv;
return nv;
}
};
template<class T>
class CNullableT
{
public:
CNullableT(CNullValue &v) : m_Value(T()), m_IsNull(true)
{
}
CNullableT(T value) : m_Value(value), m_IsNull(false)
{
}
bool IsNull()
{
return m_IsNull;
}
T GetValue()
{
return m_Value;
}
private:
T m_Value;
bool m_IsNull;
};
This is how I'll have to define functions:
void StoredProc(int i, CNullableT<int> j)
{
...connect to database
...if j.IsNull pass null to database etc
}
And I call it like this:
sp.StoredProc(1, 2);
or
sp.StoredProc(3, CNullValue::Null());
I was just wondering if there was a better way than this. In particular I don't like the singleton-like object of CNullValue with the statics.
I'd prefer to just do
sp.StoredProc(3, CNullValue);
or something similar. How do others solve this problem?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…