【发布时间】:2015-01-25 22:21:12
【问题描述】:
所以背景,我在 Java 编程语言方面有很强的背景。谈到 C++ 语言,我完全是个菜鸟。基本上我知道在 C++ 中定义构造函数有两种不同的语法,我只是不明白其中的区别。
如下编写异常构造函数时(类似于没有返回类型的方法),编译器输出no matching function for call to 'std::runtime_error::runtime_error()'错误:
#include <stdexcept>
using namespace std;
class DivideByZeroException : public runtime_error {
public :
DivideByZeroException() {
runtime_error("attempted to divide by zero.");
}
};
但是,当构造函数修改为如下时,代码编译无错误:
#include <stdexcept>
using namespace std;
class DivideByZeroException : public runtime_error {
public :
DivideByZeroException() :
runtime_error("attempted to divide by zero."){};
};
有人可以解释一下这里发生了什么吗?
【问题讨论】:
标签: c++ class c++11 constructor runtime-error