标签:style blog http color 使用 文件
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
经验:支持”编译依存性最小化“的一般构想是:相依于声明式,不要相依于定义式。
基于此构想的两个手段是 Handle classes 和 Interface classes.
示例:相依于定义式
#include <string>
#include "date.h"
#include "address.h"
class Person{
public:
	Person(const std::string &name, const Data &birthday, const Address &addr);
	std::string name() const;
	std::string birthDate() const;
	std::string address() const;
	//...
private:
	std::string theName; //实现细目
	Date theBirthDate;   //实现细目 
	Address theAddress;  //实现细目
}#include <string>
#include <memory>
class PersonImpl;  //Person实现
class Date;
class Address;
class Person{ //Person 接口
public:
	Person(const std::string &name, const Date &birthday, const Address &addr);
	std::string name() const;
	std::string birthDate() const;
	std:;string address() const;
	//...
private:
	std::tr1::shared_ptr<PersonImpl> pImpl; //指针,指向实现物;
}#include "Person.h"
#include "PersonImpl.h" //实现 Person class 必须 include Person 和 PersonImpl 的声明式。
						//? 不应该 include PersonImpl.cpp, 不然怎么调用其成员函数
Person::Person(const std::string &name, const Date &birthday, const Address &addr)
	:pImpl(new PersonImpl(name, birthday, addr)
{};
std::string Person::name() const {
	return pImpl->name();
}class Person{
public:
	
	static std::tr1::shared_ptr<Person> //返回一个tr1::shared_ptr,指向一个新的person,Item 18说这能消除客户的资源管理责任
		create(const std::string &name, const Date &birthday, const Address &addr);
	virtual ~Person();
	virtual std::string name() const = 0;
	virtual std::string birthDate() const = 0;
	virtual std::string address() const = 0;
	//...
};
class RealPerson: public Person{
	//...
}
std::tr1::shared_ptr<Person> Person::create(const std::string &name,
											const Date &birthday,
											const Address &addr)
{
	return std::tr1::shared_ptr<Person>(new RealPerson(name, birthday, addr));
}Effective C++ Item 31 降低文件间编译依存关系,布布扣,bubuko.com
Effective C++ Item 31 降低文件间编译依存关系
标签:style blog http color 使用 文件
原文地址:http://blog.csdn.net/zhengsenlie/article/details/37658239