首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >“类型为'A‘的临时函数具有受保护的析构函数”,但其类型为B

“类型为'A‘的临时函数具有受保护的析构函数”,但其类型为B
EN

Stack Overflow用户
提问于 2019-06-25 08:16:13
回答 1查看 6.1K关注 0票数 11

在使用Clang 8.0.0+和-std=c++17编译的以下代码中,使用B{}创建派生类实例会产生错误error: temporary of type 'A' has protected destructor。当临时的类型为B (因此应该有一个公共析构函数)时,为什么A会出现在此消息中?

https://godbolt.org/z/uOzwYa

代码语言:javascript
复制
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;
}
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56745324

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档