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 forX
‘s direct non-variant non-static data members,the destructors forX
‘s direct base classes and,ifX
is the type of the most derived class,its destructor calls the destructors forX
‘s virtual base classes.
mystream是Foo的非变体,非静态数据成员(变体数据成员是union的成员; Foo不是union).