类的一个成员变量也是类,老提示没有构造函数
这是com本质论1.5节的实例 接口和实现分离
FastString是实现类,FastStringItf是接口类。要从DLL只导出FastStringItf类。只能给用户一个dll和FastStringItf的头文件,不能让用户感觉FastString类的存在。
//faststring.h是FastString类的声明class FastString{ char *m_psz;public: FastString(const char *psz); ~FastString(void); int Length(void)const; int Find(const char ch)const;};//faststring.cpp是FastString类的实现#include "FastString.h"#include <string.h>FastString::FastString(const char *psz) :m_psz(new char[strlen(psz)+1]){ strcpy(m_psz,psz);}//这里省去不必展示的代码//FastStringItf.h是FastStringItf的声明#ifdef DLLEXPORT#define DLLEXPORT __declspec(dllexport)#else#define DLLEXPORT __declspec(dllimport)#endifclass DLLEXPORT FastStringItf{ class FastString; //导入实现类的名称 FastString *m_pThis;public: FastStringItf(const char *psz); ~FastStringItf(void); int Length(void)const; int Find(const char ch)const;};//FastStringItf.cpp是类FastStringItf的实现#include "FastString.h"#include "FastStringItf.h"#include <assert.h>#include <Windows.h>BOOL APIENTRY DLLMain(HMODULE hModule,DWORD ul_reason_for_call, LPVOID lpReserved){ switch (ul_reason_for_call) { case DLL_PROCESS_ATTACH: case DLL_THREAD_ATTACH: case DLL_THREAD_DETACH: case DLL_PROCESS_DETACH: break; } return TRUE;}FastStringItf::FastStringItf(const char *psz) :m_pThis(new FastString(psz))//就是这里老提示FastString没有构造函数{ assert(m_pThis!=NULL);}//这里省去不必展示的代码