抽象基类实现接口一、 创建接口文件.h
#ifndef _DBSERVER_INCLUDE #define _DBSERVER_INCLUDE typedef long HRESULT; #define DEF_EXPORT _declspec(dllexport) class IDB { //Inte***ces public: //Inte***ce for data access virtual HRESULT Read(short nTable,short nRow,LPWSTR lpszData)=0; virtual HRESULT Write(short nTable,short nRow,LPCWSTR lpszData)=0; //Inte***ce for database management virtual HRESULT Create(short &nTable,LPCWSTR lpszName)=0; virtual HRESULT Delete(short nTable)=0; //Inte***ce for database information virtual HRESULT GetNumTables(short &nNumTables)=0; virtual HRESULT GetTableName(short nTable,LPWSTR lpszName)=0; virtual HRESULT GetNumRows(short nTable,short &nRows)=0; virtual ULONG Release()=0; }; class IDBSrvFactory { //Inte***ce public: virtual HRESULT CreateDB(IDB** ppObject)=0; virtual ULONG Release()=0; }; HRESULT DEF_EXPORT DllGetClassFactoryObject(IDBSrvFactory** ppObject); #endif 二、 建立对象程序的头文件.h 并进行映射,函数声明顺序和参数跟接口文件一致 #ifndef _DBSERVERIMP_INCLUDE #define _DBSERVERIMP_INCLUDE #include "..\Inte***ce\DBsrv.h" typedef long HRESULT; //#define DEF_EXPORT _declspec(dllexport) class CDB:public IDB { //Inte***ces public: //Inte***ce for data access HRESULT DEF_EXPORT Read(short nTable,short nRow,LPWSTR lpszData); HRESULT DEF_EXPORT Write(short nTable,short nRow,LPCWSTR lpszData); //Inte***ce for database management HRESULT DEF_EXPORT Create(short &nTable,LPCWSTR lpszName); HRESULT DEF_EXPORT Delete(short nTable); //Inte***ce for database information HRESULT DEF_EXPORT GetNumTables(short &nNumTables); HRESULT DEF_EXPORT GetTableName(short nTable,LPWSTR lpszName); HRESULT DEF_EXPORT GetNumRows(short nTable,short &nRows); ULONG DEF_EXPORT Release(); //Implementation private: CPtrArray m_arrTables;//Array of pointers to CStringArray(the "database") CStringArray m_arrNames;//Array of table names public: ~CDB(); }; class CDBSrvFactory:public IDBSrvFactory { //Inte***ce public: HRESULT DEF_EXPORT CreateDB(IDB** ppObject); ULONG DEF_EXPORT Release(); }; HRESULT DEF_EXPORT DllGetClassFactoryObject(CDBSrvFactory** ppObject); #endif 三、 建立对象的实现文件 参数保持一致 HRESULT CDBSrvFactory::CreateDB(IDB** ppObject) { *ppObject=(IDB*)new CDB; return NO_ERROR; } HRESULT DEF_EXPORT DllGetClassFactoryObject(IDBSrvFactory** ppObject) { *ppObject=(IDBSrvFactory*)new CDBSrvFactory; return NO_ERROR; } from: http://www.cppblog.com/kenbardy/archive/2006/03/14/4146.html(苍羽的学习库) |