这个问题是因为这个问题和评论。
这个例子:
#include <iostream>
struct A {
A(int value) : m_value(value) { }
int m_value;
};
struct B : A {
B(int value) : A (value) { }
};
int main()
{
try {
throw B(5);
}
catch(A) {
std::cout << "A catch" << std::endl;
}
catch(B) {
std::cout << "B catch" << std::endl;
}
}
当使用 g++ 4.6.1 像这样编译时:
g++ exception_slicing.cpp -ansi -pedantic -Wall -Wextra
产生下一个输出:
exception_slicing.cpp: In function 'int main()':
exception_slicing.cpp:20:5: warning: exception of type 'B' will be caught [enabled by default]
exception_slicing.cpp:17:5: warning: by earlier handler for 'A' [enabled by default]
输出是A catch
。
我了解由于切片问题而触发了第一个 catch 块。
- 它在哪里说基类中的隐藏复制构造函数?
- 它在哪里说明了这种行为?
PS1 请提供标准引用的答案。
PS2 而且我知道异常应该由 const 引用处理。