首页 > 解决方案 > C++ 继承:构造函数中的字符串构造

问题描述

我现在正在学习 C++ 并正在实现一个用于异常处理的示例程序。主程序实现了一个具有任意基数的数字系统。我做了一个nums_exception : public std::exception功能类virtual const char *what();

#include <stdexcept>
#include <sstream>

class Nums
{
public:
    Nums() {}
    unsigned int getBase() { return 2; }
};

class nums_exception: public std::exception
{
public:
    nums_exception(Nums* obj, std::string errortext);
    virtual ~nums_exception();

    virtual const char *what() const noexcept;
    const Nums *failObj();

private:
    std::string errortext;
    Nums *obj;
};

nums_exception::nums_exception(Nums *obj, std::string errortext)
    : errortext(errortext)
    , obj(obj)
{
}

nums_exception::~nums_exception() = default;

const char *nums_exception::what() const noexcept
{
    if (this->errortext.size() == 0)
    {
        return "Nums exception!";
    }
    else
    {
        std::stringstream ret;
        ret << "Nums exception at "
            << obj
            << " :"
            << this->errortext;
        return ret.str().c_str();
    }
}

// Now i derived  nums_bad_digit, which should look like this:

class nums_bad_digit: public nums_exception
{
public:
    nums_bad_digit(Nums* obj, uint base, char digit);
    virtual ~nums_bad_digit() override;

    static std::string ERRORTEXT(Nums *obj, char digit);

private:
    std::string errortext;
    const uint base;
    Nums *obj;
};

inline std::string nums_bad_digit::ERRORTEXT(Nums *obj, char digit)
{
    return std::string(std::to_string(digit) + " not in alphabet for base " +
    std::to_string(obj->getBase()) + '.');
}

nums_bad_digit::nums_bad_digit(Nums *obj, uint base, char digit)
    : nums_exception(obj, ERRORTEXT(obj, digit))
    , base(base)
{
}

nums_bad_digit::~nums_bad_digit() = default;

int main()
{
    Nums n;
    throw nums_bad_digit(&n, 42, 'x');
}

在这里,我尝试使用静态方法达到我的目标。

我想构造一个准备显示的错误消息,它说明了我为什么抛出这个异常,然后将它传递给nums_exception构造函数。例如, nums_bad_digit::what() 应该 在 0x777fff 返回 Nums_expection: 'Q' not in letters for base 16

我还尝试了一个编译器宏......但无论我尝试什么 - 将其编写为普通代码都可以正常工作。但是当我想将字符串传递给nums_exception构造函数时,它总是会得到一个空字符串。

标签: c++stringstdstring

解决方案


这里有几个问题。

  1. std::to_string不带char

    该参数可能会被提升为int,其值是输入的 ASCII 代码(或您使用的任何内容)。就这样'x'变成了"120"。不是你想要的。

    你应该std::string(1, digit)改用。

    阅读您使用的功能的文档!至少,单独测试程序的单元。

     

  2. what()返回 a const char*,但它指向的数据已死。

    stringstream 是本地的,而字符串是临时的,所以这个指针立即悬空。我会在构造函数中生成完整的字符串,并将其存储为成员,以便它在异常对象存在的情况下存活。

     

所以这:

nums_exception::nums_exception(Nums *obj, std::string errortext)
    : obj(obj)
{
    if (errortext.size() == 0)
    {
        this->errortext = "Nums exception!";
    }
    else
    {
        std::stringstream ret;
        ret << "Nums exception at "
            << obj
            << " :"
            << errortext;
        this->errortext = ret.str();
    }
}

和这个:

const char *nums_exception::what() const noexcept
{
    return errortext.c_str();
}

和这个:

inline std::string nums_bad_digit::ERRORTEXT(Nums *obj, char digit)
{
    return std::string(1, digit) + " not in alphabet for base " +
    std::to_string(obj->getBase()) + '.';
}

现场演示


推荐阅读