首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

一个编译倚赖的有关问题

2012-02-13 
一个编译倚赖的问题在effectivec++中提到下面的模式来消除编译倚赖,把接口和实现相分离,我的问题是,既然Pe

一个编译倚赖的问题
在effective   c++中提到下面的模式来消除编译倚赖,把接口和实现相分离,我的问题是,既然Person::makePerson中用到了“new   RealPerson(。。。。)”,那么RealPerson的改变仍然会引起Person   的重新编译,作者的目的没有达到阿

接口类:
class   Person   {
  public:   virtual   ~Person();
virtual   string   name()   const   =   0;  
virtual   string   birthDate()   const   =   0;  
virtual   string   address()   const   =   0;  
virtual   string   nationality()   const   =   0;  
static   Person   *   makePerson(const   string&   name,   const   Date&   birthday,   const   Address&   addr,   const   Country&   country);

};

Person   *   Person::makePerson(const   string&   name,   const   Date&   birthday,   const   Address&   addr,   const   Country&   country)   {  
        return   new   RealPerson(name,   birthday,   addr,   country);   }

实现类:
class   RealPerson:   public   Person   {  
    public:   RealPerson(const   string&   name,   const   Date&   birthday,   const         Address&   addr,   const   Country&   country)   :   name_(name),   birthday_(birthday),   address_(addr),   country_(country)   {}
virtual   ~RealPerson()   {}
string   name()   const;nationality()   const;
private:   string   name_;   Date   birthday_;   Address   address_;   Country   country_;




[解决办法]
这里的makePerson实际上是一个factory,楼主注意,它是static成员函数,其实它并不属于Person类,完全可以从类中脱离出来作为一个名字空间级函数来定义。

热点排行
Bad Request.