dtor15.c

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

C
56
字号
#include "fail.h"
#include <string.h>

int count;

struct S {
    char *s;
    S( char * );
    S( S const & );
    ~S();
    int operator ==( S const & );
};

S::S( char *m ) : s( new char[ strlen(m) + 1 ] ) {
    ++count;
    strcpy( s, m );
}

S::S( S const &r ) : s( new char[ strlen(r.s) + 1 ] ) {
    ++count;
    strcpy( s, r.s );
}

S::~S() {
    --count;
    if( count < 0 ) fail(__LINE__);
    delete s;
}

int S::operator ==( S const &r )
{
    return strcmp( s, r.s ) == 0;
}

void check( int v )
{
    if( v != 0 ) fail(__LINE__);
}

#define MYASSERT( bb )	(bb)?(void)0:check((bb))

int main()
{
    {
	S x("1");
	S y("2");
    
	MYASSERT( x == "1" );
	if( count != 2 ) fail(__LINE__);
	MYASSERT( y == "1" );
	if( count != 2 ) fail(__LINE__);
    }
    if( count != 0 ) fail(__LINE__);
    _PASS;
}

⌨️ 快捷键说明

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