加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C级成员的一生

发布时间:2020-12-16 07:53:13 所属栏目:百科 来源:网络整理
导读:C类成员的生命周期是多少.例如,何时释放Foo对象的std :: fstream?进入析构函数或离开析构函数时?这是在C标准中定义的吗? struct Foo{ std::fstream mystream; ~Foo() { // wait for thread writing to mystream }}; 解决方法 在执行~Foo()的主体之后,在销
C类成员的生命周期是多少.例如,何时释放Foo对象的std :: fstream?进入析构函数或离开析构函数时?这是在C标准中定义的吗?
struct Foo
{
    std::fstream mystream;
    ~Foo()
    {
        // wait for thread writing to mystream
    }
};

解决方法

在执行~Foo()的主体之后,在销毁Foo对象期间销毁mystream数据成员. C11§12.4[class.dtor] / 8州:

After executing the body of the destructor and destroying any automatic objects allocated within the body,a destructor for class X calls the destructors for X‘s direct non-variant non-static data members,the destructors for X‘s direct base classes and,if X is the type of the most derived class,its destructor calls the destructors for X‘s virtual base classes.

mystream是Foo的非变体,非静态数据成员(变体数据成员是union的成员; Foo不是union).

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读