C级成员的一生

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).

相关文章

对象的传值与返回说起函数,就不免要谈谈函数的参数和返回值...
从实现装饰者模式中思考C++指针和引用的选择最近在看...
关于vtordisp知多少?我相信不少人看到这篇文章,多半是来自...
那些陌生的C++关键字学过程序语言的人相信对关键字并...
命令行下的树形打印最近在处理代码分析问题时,需要将代码的...
虚函数与虚继承寻踪封装、继承、多态是面向对象语言的三大特...