test.h
#pragma once
#include "shared_ptr.hpp"
class CTest
{
public:
CTest(void);
~CTest(void);
void DoSomething();
private:
class CTestImp;
boost::shared_ptr<CTestImp> pimpl_;
};test.cpp
#include "Test.h"
#include <iostream>
class CTest::CTestImp
{
private:
CTestImp(CTestImp const &){}
CTestImp & operator=(CTestImp const &){}
public:
CTestImp(){}
void DoSomething();
};
void CTest::CTestImp::DoSomething()
{
// do something.
std::cout<<"Imp class do something."<<std::endl;
}
CTest::CTest(void)
{
boost::shared_ptr<CTestImp> pImp(new CTestImp);
pimpl_ = pImp;
}
CTest::~CTest(void)
{
}
void CTest::DoSomething()
{
pimpl_->DoSomething();
}方法二:使用抽象类来实现接口与实现的分离。
x.h
#pragma once
#include <stdio.h>
#include "shared_ptr.hpp"
using namespace boost;
class X
{
public:
virtual void f() = 0;
virtual void g() = 0;
protected:
~X() { printf("~X\n");}
};
shared_ptr<X> createX();x.cpp
#include "X.h"
#include <stdio.h>
class X_impl: public X
{
private:
X_impl(){};
X_impl(X_impl const &);
X_impl & operator=(X_impl const &);
public:
~X_impl(){printf("~X_impl\n");};
virtual void f()
{
printf("X_impl.f()\n");
}
virtual void g()
{
printf("X_impl.g()\n");
}
private:
friend shared_ptr<X> createX();
};
shared_ptr<X> createX()
{
shared_ptr<X> px(new X_impl);
return px;
}


CTestImp(CTestImp 















