#include "stdafx.h" #include #include #include #include using namespace std; using namespace boost; struct Hello { Hello () { cout << "Hello::Hello () @ " << this << endl; } Hello (const Hello&) { cout << "Hello::Hello (const ref) @ " << this << endl; } ~Hello () { cout << "Hello::~Hello @ " << this << endl; } Hello& operator= (const Hello&) const { cout << "Hello::operator= @ " << this << endl; } void operator () (int i) { cout << "i = " << i << endl; } }; void copytest () { cout << "copy test:\n"; Hello hello; cout << "\nbefore declaration:\n"; boost::signal sig; cout << "\nafter declaration:\n"; cout << "\nbefore connect:\n"; sig.connect (hello); cout << "\nafter connect:\n"; cout << "before sig send:\n"; sig (1968); cout << "after sig send:\n"; } void reftest () { cout << "ref test:\n"; Hello hello; cout << "\nbefore declaration:\n"; boost::signal sig; cout << "\nafter declaration:\n"; cout << "\nbefore connect:\n"; sig.connect (boost::ref (hello)); cout << "\nafter connect:\n"; cout << "\nbefore sig send:\n"; sig (1968); cout << "\nafter sig send:\n"; } /* ALLOCATION HOOK FUNCTION ------------------------- An allocation hook function can have many, many different uses. This one simply logs each allocation operation in a file. */ int __cdecl MyAllocHook( int nAllocType, void * pvData, size_t nSize, int nBlockUse, long lRequest, const unsigned char * szFileName, int nLine ) { char *operation[] = { "", "allocating", "re-allocating", "freeing" }; char *blockType[] = { "Free", "Normal", "CRT", "Ignore", "Client" }; if ( nBlockUse == _CRT_BLOCK ) // Ignore internal C runtime library allocations { return( true ); } _ASSERT( ( nAllocType > 0 ) && ( nAllocType < 4 ) ); _ASSERT( ( nBlockUse >= 0 ) && ( nBlockUse < 5 ) ); printf( "Memory operation in %s, line %d: %s a %d-byte '%s' block (# %ld)\n", szFileName, nLine, operation[nAllocType], nSize, blockType[nBlockUse], lRequest ); if ( pvData != NULL ) printf( " at %x", (int)pvData ); return( true ); // Allow the memory operation to proceed } // make this a main() for stand alone int copy_and_ref_test() { //_CrtSetDumpClient( MyDumpClientHook ); //_CrtSetReportHook( MyReportHook ); _CrtSetAllocHook( MyAllocHook ); copytest (); reftest (); return 0; }