c++ - 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
构造函数时,它总是会得到一个空字符串。
解决方案
这里有几个问题。
-
该参数可能会被提升为
int
,其值是输入的 ASCII 代码(或您使用的任何内容)。就这样'x'
变成了"120"
。不是你想要的。你应该
std::string(1, digit)
改用。阅读您使用的功能的文档!至少,单独测试程序的单元。
what()
返回 aconst 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()) + '.';
}
(现场演示)
推荐阅读
- visual-studio - 框架 4.6.2 在 Visual Studio 2015 中不可用?
- asp.net-mvc - 发布时无法运行 MVC Core 项目
- java - ProgressDialog 始终不确定,即使最初将样式设置为 STYLE_HORIZONTAL
- javascript - 使用 Firebase 云功能推送通知
- javascript - 如何在导航栏中右对齐文本 - HTML
- android - “预览”屏幕中的Android空白屏幕
- c++ - c++如何评估赋值运算符(if语句)
- python - 带有日期时间索引的 Pandas Pivot
- javascript - 如何在Javascript中过滤对象数组
- javascript - 通过右键单击、Applescript 和/或 Javascript 访问浏览器中的链接?