返回“NULL””如果找不到搜索结果,则为对象

我对C ++很陌生,所以在我学习的时候,我倾向于设计很多Java主义。无论如何,在Java中,如果我的类具有“搜索”方法,该方法将从匹配特定参数的
Collection< T >
返回对象
T
,我将返回该对象,如果在集合中找不到该对象,我将返回
null
。然后在我的通话功能中,我只需检查
if(tResult != null) { ... }
在C ++中,我发现如果对象不存在,我不能返回
null
值。我只想返回一个类型为T的'指示符',它通知调用函数没有找到任何对象。我不想抛出异常,因为它并不是一个特例。 这就是我的代码现在的样子:
class Node {
    Attr& getAttribute(const string& attribute_name) const {
       //search collection
       //if found at i
            return attributes[i];
       //if not found
            return NULL; // what should this be?
    }

private:
    vector<Attr> attributes;
}
我怎样才能改变它以便我能给出那种标记?     
已邀请:
在C ++中,引用不能为null。如果要在找不到任何内容时可选地返回null,则需要返回指针,而不是引用:
Attr *getAttribute(const string& attribute_name) const {
   //search collection
   //if found at i
        return &attributes[i];
   //if not found
        return nullptr;
}
否则,如果您坚持通过引用返回,那么如果找不到该属性,则应该抛出异常。 (顺便说一下,我有点担心你的方法是
const
并返回非
const
属性。出于哲学原因,我建议你回复
const Attr *
。如果你也想修改这个属性,你可以重载一个非
const
方法也返回非
const
属性。)     
这里有几个可能的答案。您想要返回可能存在的内容。以下是一些选项,从最不喜欢的到最喜欢的: 通过引用返回,并且异常无法找到信号。
Attr& getAttribute(const string& attribute_name) const 
{
   //search collection
   //if found at i
        return attributes[i];
   //if not found
        throw no_such_attribute_error;
}
很可能找不到属性是执行的正常部分,因此不是非常特殊。对此的处理将是嘈杂的。无法返回空值,因为它具有空引用的未定义行为。 按指针返回
Attr* getAttribute(const string& attribute_name) const 
{
   //search collection
   //if found at i
        return &attributes[i];
   //if not found
        return nullptr;
}
很容易忘记检查getAttribute的结果是否是非NULL指针,并且是一个容易出错的bug。 使用Boost.Optional
boost::optional<Attr&> getAttribute(const string& attribute_name) const 
{
   //search collection
   //if found at i
        return attributes[i];
   //if not found
        return boost::optional<Attr&>();
}
boost :: optional表示这里发生了什么,并且有简单的方法来检查是否找到了这样的属性。 旁注:std :: optional最近被投票到C ++ 17中,因此在不久的将来这将是一个“标准”的东西。     
您可以轻松创建表示NULL返回的静态对象。
class Attr;
extern Attr AttrNull;

class Node { 
.... 

Attr& getAttribute(const string& attribute_name) const { 
   //search collection 
   //if found at i 
        return attributes[i]; 
   //if not found 
        return AttrNull; 
} 

bool IsNull(const Attr& test) const {
    return &test == &AttrNull;
}

 private: 
   vector<Attr> attributes; 
};
在源文件中的某处:
static Attr AttrNull;
    
如果你想要一个
NULL
返回值,你需要使用指针而不是引用。 参考文献本身不可能是
NULL
。 (请注意以后的评论海报:是的,如果真的真的尝试,你可以将引用的地址设为NULL)。 请参阅我的答案,以获取引用和指针之间的差异列表。     
正如你已经想到的那样,你不能像在Java(或C#)中那样做。这是另一个建议,您可以将对象的引用作为参数传递并返回bool值。如果在您的集合中找到结果,您可以将其分配给正在传递的引用并返回“true”,否则返回“false”。请考虑此代码。
typedef std::map<string, Operator> OPERATORS_MAP;

bool OperatorList::tryGetOperator(string token, Operator& op)
{
    bool val = false;

    OPERATORS_MAP::iterator it = m_operators.find(token);
    if (it != m_operators.end())
    {
        op = it->second;
        val = true;
    }
    return val;
}
上面的函数必须针对键'token'找到Operator,如果它找到了它返回true的那个并将值赋给参数Operator&amp;运。 此例程的调用者代码如下所示
Operator opr;
if (OperatorList::tryGetOperator(strOperator, opr))
{
    //Do something here if true is returned.
}
    
你不能在这里返回NULL的原因是因为你已经将你的返回类型声明为
Attr&
。尾随
&
使返回值成为“引用”,它基本上是指向现有对象的保证 - 不为空的指针。如果您希望能够返回null,请将
Attr&
更改为
Attr*
。     
您无法返回
NULL
,因为函数的返回类型是对象
reference
而不是
pointer
。     
你可以试试这个:
return &Type();
    

要回复问题请先登录注册