// builderTest.cpp : Defines the entry point for the console application. // #include "stdafx.h" class IAdd { public: virtual int Add(int a, int b) = 0; }; //有一个类实现了这个功能(利用已有的轮子,避免重复发明) class COtherAdd { public: int OtherAdd(int a, int b) { return a + b; } }; //下面就想办法利用这个功能 class CAdaptAdd : public IAdd { public: CAdaptAdd(COtherAdd* pOtherAdd) : m_pOtherAdd(pOtherAdd) { } virtual int Add(int a, int b) { return m_pOtherAdd->OtherAdd(a , b); } private: COtherAdd* m_pOtherAdd; }; int _tmain(int argc, _TCHAR* argv[]) { COtherAdd* pOtherAdd = new COtherAdd; IAdd* pAdaptAdd = new CAdaptAdd(pOtherAdd); pAdaptAdd->Add(10, 20); delete pAdaptAdd; delete pAdaptAdd; return 0; }