我在这里找到了一些关于Null对象模式的信息(https://softwareengineering.stackexchange.com/questions/152094/null-pointers-vs-null-object-pattern)和这里(http://en.wikipedia.org/ wiki/Null_Object_pattern#C.2B.2B).
但是,C++实现并没有说明我的用例.
我还看到了Nullable Type的相关链接(http://en.wikipedia.org/wiki/Nullable_type).
我有一个不属于层次结构的对象,通常不会在堆上分配.此外,没有一个方便的值可以用作指示null的标记.希望以下代码使用例清晰.
class ContrivedType
{
public:
ContrivedType() :
mValue(0)
{
// Do nothing
}
bool operator==(const ContrivedType& other) const
{
return mValue == other.mValue;
}
void setValue(std::uint16_t value)
{
mValue = value;
}
private:
// All values in the range [0, 65535] are valid for use
std::uint16_t mValue;
};
class Foo
{
public:
const ContrivedType getValue() const
{
return mValue;
}
void …Run Code Online (Sandbox Code Playgroud) 我有一个功能,检查正则表达式并std::vector<int>根据正则表达式结果返回.我需要检查功能是否失败/成功.在成功返回向量对象时,无法nullptr稍后检查if (somefunc() == nullptr) // do smth