1998.9.6

// This program demonstrates a couple of interesting
// problems with Borland C++ 5.x.  Subroutine Bar()
// contains a static object Baz, which should be constructed
// the first time the routine is called, and destroyed
// when the program exits.  This much works properly.
//
// The static object, Baz, is constructed using a pair
// of temporary instances of Foo.  The ISO standard dictates
// that the lifetime of these temporaries should expire at
// the end of the statement construucting Baz.  However,
// viewing the program output shows that the temporaries
// are not destroyed until Bar() exits. The should also
// be destroyed in the reverse of the order they were
// constructed.
//
// Much worse, however, is the error that occurs the second
// time that Bar() is called.  Since the static object, Baz,
// is not constructed the second time through the routine,
// the two temporaries are not created.  However, the routine
// still attempts to destroy the non-existent routines upon
// exit.  This is very bad!  Note that the bad char * value
// in the non-existent temporaries may cause your version of
// the program to crash.
//
//  Build this program using the following command line:
//
//   bcc32 apr98.cpp
//

#include <iostream.h>

class Foo {
public:
    Foo( int val, char *tag ) {
        m_val = val;
        m_tag = tag;
        cout << "constructing Foo("
             << m_val
             << ", "
             << m_tag
             << ")\n";
    }
    ~Foo() {
        cout << "destroying Foo("
             << m_val
             << ", "
             << m_tag
             << ")\n";
    }
    int m_val;
    char *m_tag;
};

ostream &operator <<( ostream &os, const Foo &f )
{
   return os << f.m_val << ", " << f.m_tag;
}

int operator +( const Foo &lhs, const Foo &rhs )
{
    return lhs.m_val + rhs.m_val;
}

void Bar()
{
    static Foo Baz =
        Foo( Foo( 1, "temp" ) + Foo( 2, "temp" ), "static" );
    cout << "Baz = " << Baz << endl;
}

int main()
{
    cout << "\nCalling Bar() for the first time...\n";
    Bar();
    cout << "\nCalling Bar() for the second time...\n";
    Bar();
    cout << "\nTerminating...\n";
    return 0;
}


TC 3.0 ++ 출력물
Calling Bar() for the first time...
constructing Foo(2, temp)
constructing Foo(1, temp)
constructing Foo(3, static)
Baz = 3, static
destroying Foo(2, temp)     // 임시클래스는 static foo Baz 문장에서 destroy
destroying Foo(1, temp)     // 되었어야 한다. ISO에 기준

Calling Bar() for the second time...
Baz = 3, static
destroying Foo(1022, )      // 있지도 않은 클래스의 디스트럭터 호출
destroying Foo(1492, )      // 프로그램을 망칠수도 있다.

Terminating...
destroying Foo(3, static)



VC 5.0 콘솔 32
Calling Bar() for the first time...
constructing Foo(2, temp)
constructing Foo(1, temp)
constructing Foo(3, static)
destroying Foo(1, temp)
destroying Foo(2, temp)
Baz = 3, static

Calling Bar() for the second time...
Baz = 3, static

'KB > C/C++' 카테고리의 다른 글

라인위의점으로선택  (0) 2004.03.19
[tc] tc 사용하기  (0) 2004.03.19
C & C++ Tip 1001  (0) 2004.03.19
비트찍기  (0) 2004.03.19
[djgpp] class 라이브러리만들기  (0) 2004.03.19

+ Recent posts