轉貼自http://www.cppblog.com/mzty/archive/2007/08/06/29441.html方法一:使用另一个实现类分装类的私有成员和函数,这种方法称为Pimpl方法。test.h #pragma once#include "shared_ptr.hpp"class CTest; test.cpp #include "Test.h"#include <iostream>class CTest::CTestImp} 方法二:使用抽象类来实现接口与实现的分离。x.h #pragma once#include <stdio.h>#include "shared_ptr.hpp"using namespace boost;class X createX(); x.cpp #include "X.h"#include <stdio.h>class X_impl: public X} 总结: 接口与实现的分离,有助于我们对代码实现的保护,特别是如果我们开发lib共别人使用使,更要注意。在实现分离的过程中,最好采用上面的第一种方法的智能指针boost::shared_ptr的实现,简单安全。 相关文章: 2022-02-10 2021-12-01 2022-01-14 2021-05-23 2021-06-17 2021-12-22 2021-12-13 2021-11-07