在使用Clang 8.0.0+和
-std=c++17
编译的以下代码中,使用B{}
创建派生类实例会出现错误error: temporary of type 'A' has protected destructor
。为什么该临时对象的类型是B
(因此应具有公共析构函数),但错误消息中出现了A
?
class A {
protected:
A() = default;
~A() = default;
};
class B : public A {
// can also omit these 3 lines with the same result
public:
B() = default;
~B() = default;
};
void foo(const B&) {}
int main() {
// error: temporary of type 'A' has protected destructor
foo(B{});
// ^
return 0;
}
clang --version
,会显示什么内容? - Cruz JeanB
继承了拥有受保护析构函数的A
。 - user10957435