TryCObArray.cpp
来源:互联网 发布:北京淘宝运营招聘 编辑:程序博客网 时间:2024/05/04 19:20
// TryCObArray.cpp : Defines the entry point for the console application.//#include "stdafx.h"#include "TryCObArray.h"#ifdef _DEBUG#define new DEBUG_NEW#endif// 唯一的应用程序对象CWinApp theApp;using namespace std;class CElement :public CObject{public: CElement(CString pType) { m_Type = pType ; TRACE("构造 *** %s\n", m_Type) ; } ~CElement() { TRACE("析构 --> %s\n", m_Type) ; } CString& GetContent() { return m_Type ; }public: CString m_Type ;} ;int _tmain(int argc, TCHAR* argv[], TCHAR* envp[]){ int nRetCode = 0; // 初始化 MFC 并在失败时显示错误 if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0)) { // TODO: 更改错误代码以符合您的需要 _tprintf(_T("错误: MFC 初始化失败\n")); nRetCode = 1; } else { // TODO: 在此处为应用程序的行为编写代码。 CObArray obaArray ; for ( int i = 0 ; i < 10 ; i++ ) { CElement * pElement ; CString strTemp ; strTemp.Format(_T("%d"), i) ; pElement = new CElement(strTemp) ; obaArray.Add(pElement) ; } for ( i = 0 ; i < 10 ; i++ ) { CElement * pElement ; //pElement = (CElement*)obaArray.GetAt(i) ;pElement = (CElement*)obaArray[i] ;#ifdef UNICODE wcout << "访问到的元素是 " << (LPCTSTR)pElement->m_Type << endl ;#else cout << "访问到的元素是 " << (LPCTSTR)pElement->m_Type << endl ;#endif } // 使用操作符[]来访问元素 for ( i = 0 ; i < 10 ; i++ ) { CElement * pElement ; pElement = (CElement*)obaArray[i] ; delete pElement ; } obaArray.RemoveAll() ; } return nRetCode;}