Return a “NULL” object if search result not found

前端 未结 8 2118
一个人的身影
一个人的身影 2020-11-28 02:23

I\'m pretty new to C++ so I tend to design with a lot of Java-isms while I\'m learning. Anyway, in Java, if I had class with a \'search\' method that would return an object

相关标签:
8条回答
  • 2020-11-28 03:03

    The reason that you can't return NULL here is because you've declared your return type as Attr&. The trailing & makes the return value a "reference", which is basically a guaranteed-not-to-be-null pointer to an existing object. If you want to be able to return null, change Attr& to Attr*.

    0 讨论(0)
  • 2020-11-28 03:04

    You can try this:

    return &Type();
    
    0 讨论(0)
  • 2020-11-28 03:06

    As you have figured out that you cannot do it the way you have done in Java (or C#). Here is another suggestion, you could pass in the reference of the object as an argument and return bool value. If the result is found in your collection, you could assign it to the reference being passed and return ‘true’, otherwise return ‘false’. Please consider this code.

    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;
    }
    

    The function above has to find the Operator against the key 'token', if it finds the one it returns true and assign the value to parameter Operator& op.

    The caller code for this routine looks like this

    Operator opr;
    if (OperatorList::tryGetOperator(strOperator, opr))
    {
        //Do something here if true is returned.
    }
    
    0 讨论(0)
  • 2020-11-28 03:07

    You are unable to return NULL because the return type of the function is an object reference and not a pointer.

    0 讨论(0)
  • 2020-11-28 03:21

    If you want a NULL return value you need to use pointers instead of references.

    References can't themselves be NULL.

    (Note to the future comment posters: Yes you can have the address of a reference be NULL if you really really try to).

    See my answer here for a list of differences between references and pointers.

    0 讨论(0)
  • 2020-11-28 03:24

    There are several possible answers here. You want to return something that might exist. Here are some options, ranging from my least preferred to most preferred:

    • Return by reference, and signal can-not-find by exception.

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

    It's likely that not finding attributes is a normal part of execution, and hence not very exceptional. The handling for this would be noisy. A null value cannot be returned because it's undefined behaviour to have null references.

    • Return by pointer

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

    It's easy to forget to check whether a result from getAttribute would be a non-NULL pointer, and is an easy source of bugs.

    • Use 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&>();
      }

    A boost::optional signifies exactly what is going on here, and has easy methods for inspecting whether such an attribute was found.


    Side note: std::optional was recently voted into C++17, so this will be a "standard" thing in the near future.

    0 讨论(0)
提交回复
热议问题