ctor07.c

来自「开放源码的编译器open watcom 1.6.0版的源代码」· C语言 代码 · 共 35 行

C
35
字号
#include "fail.h"

unsigned dtors;
unsigned ctors;

struct S {
    static S *a[100];
    static int n;
    S(int x = 0) {
	a[n++] = this;
	++ctors;
    }
    ~S() {
	++dtors;
	if( a[--n] != this ) fail(__LINE__);
    }
};

S * S::a[];
int S::n;

struct F {
    ~F() {
	if( !( dtors == 3 && errors == 0 ) ) fail(__LINE__);
    }
} final;

S d3[] = { 1, 2, 3 };

int main()
{
    if( ctors != 3 ) fail(__LINE__);
    _PASS;
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?