새로운 할당 된 개체의 수명

할당 된 개체에는 연산자 정의 된 범위가 종료 될 때 파괴 되지 않습니다.때문에 연산자 개체에 할당에 대 한 포인터를 반환 하 고 프로그램에 대 한 포인터를 이러한 개체에 액세스 하는 적절 한 범위를 정의 해야 합니다.예를 들면 다음과 같습니다.

// expre_Lifetime_of_Objects_Allocated_with_new.cpp
// C2541 expected
int main()
{
    // Use new operator to allocate an array of 20 characters.
    char *AnArray = new char[20];

    for( int i = 0; i < 20; ++i )
    {
        // On the first iteration of the loop, allocate
        //  another array of 20 characters.
        if( i == 0 )
        {
            char *AnotherArray = new char[20];
        }
    }

    delete [] AnotherArray; // Error: pointer out of scope.
    delete [] AnArray;      // OK: pointer still in scope.
}

한 번 마우스 포인터 AnotherArray 예제에서는 개체가 범위를 벗어난 이동 삭제할 수 없습니다.

참고 항목

참조

새 운영자 (C++)