new17.c

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

C
76
字号
#include "fail.h"

unsigned order;
unsigned count;

struct Dtorable {
    int _order;
    Dtorable();
    Dtorable( Dtorable const & );
    ~Dtorable();
};

Dtorable::Dtorable()
    : _order( ++order )
{
}

Dtorable::Dtorable( Dtorable const & )
    : _order( ++order )
{
}

Dtorable::~Dtorable()
{
    if( order != _order ) _fail;
    -- order;
}

struct Composed {
    Dtorable d1;
    Dtorable d2;
};

int count_other = 0;

struct AnotherDtorable {
    AnotherDtorable();
    ~AnotherDtorable();
};

AnotherDtorable::AnotherDtorable()
{
    ++ count_other;
}

AnotherDtorable::~AnotherDtorable()
{
    -- count_other;
}


char some_data[ sizeof( Composed ) ];

//inline
Composed* ctor( Composed const & src )
{
    AnotherDtorable stuff_1;
    AnotherDtorable stuff_2;
    AnotherDtorable stuff_3;
    Composed* ptr = new( some_data ) Composed( src );
    return ptr;
}


int main()
{
    if( order != 0 ) _fail;
    {
        Composed comp;
        Composed* ptr = ctor( comp );
        ptr->~Composed();
    }
    if( order != 0 ) _fail;
    _PASS;
}

⌨️ 快捷键说明

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