www.pudn.com > zfcdcl.rar > vdtor.cpp, change:2005-07-11,size:1379b


//////////////////////////////////////////////////////////////// 
// MSDN Magazine -- November 2005 
// If this code works, it was written by Paul DiLascia. 
// If not, I don't know who wrote it. 
// Compiles with Visual Studio .NET 2003 (V7.1) on Windows XP. Tab size=3. 
// 
// Program to illustrate implicit virtualness of managed destrcutors. 
// To compile, type: 
// 
//		cl /clr vdtor.cpp 
// 
#include <tchar.h> 
#include <stdio.h> 
 
#using <mscorlib.dll> 
using namespace System; 
 
////////////////// 
// Manged base class. 
// 
public __gc class CBase { 
public: 
	CBase() 
	{ 
		printf("ctor: CBase\n"); 
	} 
 
	//	virtual keyword unnecessary for managed class because CBase is 
	//	implicitly derived from Object and the dtor is converted to a Finalize 
	//	method, which is virtual. 
 
	/*virtual*/ ~CBase() 
	{ 
		printf("dtor: CBase\n"); 
	} 
}; 
 
////////////////// 
// Manged derived class. 
// 
public __gc class CDerived : public CBase { 
public: 
	CDerived() 
	{ 
		printf("ctor: CDerived\n"); 
	} 
	~CDerived() 
	{ 
		printf("dtor: CDerived\n"); 
	} 
}; 
 
////////////////// 
// Prgram entry point. 
// 
int _tmain() 
{ 
	// Create object: note pointer is declared as CBase* but actually points 
	// to instance of derived class. 
	CBase* pBase = new CDerived(); 
 
	// Explicitly delete to see which dtor is called...? 
	delete pBase; 
 
	return 0; 
}