
//
// This is example code from Chapter 18.3.1 "Debugging constructors and destructors" of
// "Programming -- Principles and Practice Using C++" by Bjarne Stroustrup
//

#include "std_lib_facilities.h"

//------------------------------------------------------------------------------

struct X {    // simple test class
    int val;

    void out(const string& s)
    { cerr << this <<  "->" << s << ": " << val << "\n"; }

    X() { 
		val=17; 
		out("X()"); 
	}
    X(int v) { 
		val=v; 
		out( "X(int)"); 
	}
    X(const X& x){ 
		out("X(X&) "); 
		val=x.val; 
	}
    X& operator=(const X& a) {
		val = a.val;
		out("X::operator=()"); 
		return *this; 
	}
    ~X() { 
		out("~X()"); 
	}
};

//------------------------------------------------------------------------------

X glob(2);  // a global variable decl will print first line of output (prints "2")

X copy(X a) { return a; }  // definition of copy()

X copy2(X a) { X aa = a; return aa; } // definition of copy2()

X& ref_to(X& a) { return a; } // definition of ref_to

X* make(int i) { X a(i); return new X(a); } //definition of make()

struct XX { X a; X b; };

//------------------------------------------------------------------------------

int main()
{
	cout << "hi mom1\n";
    X loc(4);        // local variable prints 4
	cout << "hi mom2\n";
    X loc2 = loc;
	cout << "hi mom3\n";
    loc = X(5);
	cout << "hi mom4\n";
    loc2 = copy(loc);
	cout << "hi mom5\n";
    loc2 = copy2(loc);
	cout << "hi mom6\n";
    X loc3(6);
	cout << "hi mom7\n";
    X& r = ref_to(loc3);
	cout << "hi mom8\n";
    delete make(7);
	cout << "hi mom9\n";
    delete make(8);
	cout << "hi mom10\n";
    vector<X> v(4);
	cout << "hi mom11\n";
    XX loc4;
	cout << "hi mom12\n";
    X* p = new X(9);    // an X on the free store
	cout << "hi mom13\n";
    delete p;
	cout << "hi mom14\n";
    X* pp = new X[5];    // an array of Xs on the free store
	cout << "hi mom15\n";
    delete[] pp;
}

//------------------------------------------------------------------------------
