Can a destructor be recursive?

后端 未结 5 755
北荒
北荒 2021-02-01 00:55

Is this program well-defined, and if not, why exactly?

#include 
#include 
struct X {
    int cnt;
    X (int i) : cnt(i) {}
    ~X()          


        
5条回答
  •  温柔的废话
    2021-02-01 01:29

    Yeah, that sounds about right. I would think once the destructor is finished calling, the memory would be dumped back into the allocatable pool, allowing something to write over it, thus potentially causing issues with follow-up destructor calls (the 'this' pointer would be invalid).

    However, if the destructor doesn't finish until the recursive loop is unwound.. it should theoretically be fine.

    Interesting question :)

提交回复
热议问题