What should the return type be when the function might not have a value to return?

后端 未结 6 1031
被撕碎了的回忆
被撕碎了的回忆 2021-01-01 17:25

In the old days, you might have a function like this:

const char* find_response(const char* const id) const;

If the item could not be found

6条回答
  •  没有蜡笔的小新
    2021-01-01 18:09

    If the function is returning a string by reference, but needs the ability to indicate that no such string exists, the most obvious solution is to return a pointer, which is basically a reference that can be null, i.e. exactly what was sought after.

    const std::string* find_response(const std::string& id) const;
    

提交回复
热议问题