在使用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;
}
https://stackoverflow.com/questions/56745324
复制相似问题