简体   繁体   English

返回带有错误检查的“ const std :: string&”的方法

[英]A Method that returns “const std::string&” with error checking

I am writing a function, which I want to return "const std::string&". 我正在编写一个函数,我想返回“ const std :: string&”。 Let's just look at the codes. 让我们看一下代码。

class A
{
public:
    const std::string& GetString() const
    {
        if (list.empty())
        {
            return "Warning!";    // how to get around this line.
        }
        return list[0];
    };
protected:
    std::vector<std::string> list;
};

The above codes are an example. 上面的代码是一个示例。 The basic idea is to write a function that returns const reference, but also able to check for errors. 基本思想是编写一个返回const引用但还可以检查错误的函数。

So, how to get around "return "Warning!";"? 那么,如何解决“返回“警告!”;”?

Thanks, 谢谢,

if (list.empty()) {
    static std::string w("Warning!");
    return w;
}

since you're returning a const reference, it does not matter that you're always returning a reference to the same object. 由于返回的是const引用,因此始终返回对同一对象的引用都没关系。

If you're interested in using string references, why not take a string reference as a parameter and return a Boolean success/failure flag? 如果您对使用字符串引用感兴趣,为什么不将字符串引用作为参数并返回布尔值成功/失败标志?

class A
{
public:
    const bool GetString(std::string& outString) const
    {
        if (list.empty())
            return false;

        outString = list[0];
        return true;
    };
protected:
    std::vector<std::string> list;
};

Achieves same result, and gives a simple Boolean result. 获得相同的结果,并给出一个简单的布尔结果。

EDIT: as Ferruccio points out, this isn't an approach to be taken lightly. 编辑:正如Ferruccio指出的那样,这不是一个容易掉以轻心的方法。 Use of parameters for output in this manner is prone to causing confusion and bugs, and should be used sparingly, and be well documented where used. 以这种方式将参数用于输出容易引起混淆和错误,应谨慎使用,并在使用时进行充分记录。

If you really want to avoid using exceptions (that would be my first choice), you may consider this: 如果您真的想避免使用异常(那是我的首选),则可以考虑以下方法:

class A
{
public:
    const std::string& GetString() const
    {
        if (list.empty())
        {
            return warning;
        }
        return list[0];
    };
protected:
    std::vector<std::string> list;

private:
    static std::string warning;
};

// in *.cpp
std::string A::warning = "warning";

You might throw an exception in this function and add a function to check whether the list is empty. 您可能会在此函数中引发异常,并添加一个函数以检查列表是否为空。 Then the caller can check before calling GetString() . 然后,调用方可以在调用GetString()之前进行检查。 But if they fail to do that, they get an exception they can't ignore. 但是,如果他们未能做到这一点,他们将获得一个不可忽视的例外。

If you don't want to use exceptions (there are pros and cons of going either way), I'd first ask if it's reasonable to require the caller to call an "isValid" method first. 如果您不想使用异常(无论哪种方式都有优点和缺点),我首先会问要求调用者首先调用“ isValid”方法是否合理。

class A
{
public:
    bool IsStringValid() const
    {
         return !list.empty();
    }
    const std::string& GetString() const
    {
        if (list.empty())
        {
            return "";
        }
        return list[0];
    };
protected:
    std::vector<std::string> list;

private:
    static QString warning;
};

If it were me, I'd probably have it return a const std::string* instead so NULL is an option, or define a sentinel value as Fiktik suggested. 如果是我,我可能会让它返回一个const std::string*所以可以选择NULL ,或者按照Fiktik的建议定义一个哨兵值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM