如何使派生构造函数将串联值传递给其父构造函数
How can I make a derived constructor pass a concatenated value to its parent constructor?
我有两个异常类,一个继承自另一个:
class bmd2Exception : public std::runtime_error
{
public:
bmd2Exception(const std::string & _description) throw () : std::runtime_error(_description) {}
~bmd2Exception() throw() {}
};
class bmd2FileException : public bmd2Exception
{
public:
bmd2FileException(const std::string & _description, const char * _file, long int _line) throw()
{
std::stringstream ss;
ss << "ERROR in " << _file << " at line " << _line << ": " << _description;
bmd2Exception(ss.str());
}
~bmd2FileException() throw() {}
};
我收到的错误消息:
no matching function for call to ‘bmd2::bmd2Exception::bmd2Exception()’
我知道这是因为 bmd2FileException 的构造函数正在尝试调用尚未定义的 bmd2Exception()。 我真正想发生的是让 bmd2FileException() 调用 bmd2Exception(const std::string &) 并带有连接的错误消息。 我该怎么做?
谢谢!
一种常见的范例是创建一个帮助程序函数:
class bmd2FileException : public bmd2Exception
{
private:
static std::string make_msg(const std::string & _description, const char * _file, long int _line);
public:
bmd2FileException(const std::string & _description, const char * _file, long int _line)
: bmd2Exception(make_msg(_description, _file, _line))
{ }
};
现在只需将您的消息创建代码放在 bmd2FileException::make_msg(...)
中。
顺便说一句,如果您的构造函数正在连接字符串,我不太确定它是throw()
.
相关文章:
- 一对向量构造函数:初始值设定项列表与显式构造
- 为什么在使用转换构造函数赋值后调用C++类的析构函数?
- 获取从C++中同一类中的构造函数调用的方法返回的值
- 复制构造函数、赋值运算符C++
- 复制包含C++所有元素的对象!(构造函数和赋值,最佳实践?
- 通过构造函数创建一些值并尝试添加到文档中使用 rapidjson 不起作用
- 使用基类指针调用基类的值构造函数的语法是什么?
- C++ 基本 CTOR 说明 - 为什么不调用赋值/复制构造函数
- 从具有按值捕获的 lambda 移动构造 std::函数时,移动构造函数调用两次
- 如何在构造函数中使用初始值设定项设置具有相同值的 2d 数组?
- 数组初始值设定项的构造函数歧义
- 为用户定义的类正确调用复制构造函数/赋值运算符
- 移动构造函数和右值引用
- 赋值 boost::intrusive_ptr 而不调用构造函数?
- C++矢量复制构造函数和赋值运算符是否也复制保留空间?
- C 字符串在显式构造函数中分配值C++?
- 按值将对象传递给 SubClass 构造函数,导致超类的构造函数不调用
- 试图按值将对象传递给C++中的构造函数
- C++为什么将左值传递给move构造函数对模板有效
- 显示传递给c++类构造函数的枚举值