标签:style blog http color 使用 os
本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie
经验:多重继承比单一继承复杂。它可能导致新的歧义性,以及对 virtual 继承的需要
示例:class BorrowableItem{ public: void checkOut(); }; class ElectronicGadget{ private: bool checkOut() const; }; class MP3Player: public BorrowableItem public ElectronicGadget {...}; MP3Player mp; mp.checkOut()
mp.BorrowableItem::checkOut();
class IPerson{ //提供接口 public: static std::tr1::shared_ptr<Person> makePerson(DatabaseID personIdentifier); virtual ~IPerson(); virtual std::string name() const = 0; virtual std::string birthDate() const = 0; }; class PersonInfo{ //提供实现 public: explicit PersonInfo(DatabaseID pid); virtual ~PersonInfo(); virtual const char *theName() const; virtual const char *theBirthDate() const; virtual const char *valueDelimOpen() const; virtual const char *valueDelimClose() const; //... }; class CPerson: public Iperson, private PersonInfo{ public: explicit CPerson(DatabaseID pid): PersonInfo(pid){} virtual std::string name() const{ return PersonInfo::theName(); //利用 PersonInfo 的方法实现 IPerson 的接口 } virtual std::string birthDate() const{ return PersonInfo::theBirthDate(); } private: const char *valueDelimOpen() const {return "";} //重新定义继承而来的 virtual "界限函数" const char *valueDelimClose() const {return "";} } DatabaseID askUserForDatabaseID(); DatabaseID id(askUserForDatabaseID()); std::tr1::shared_ptr<IPerson> pp(makePerson(id));
Effective C++ Item 40 明智而审慎地使用多重继承,布布扣,bubuko.com
Effective C++ Item 40 明智而审慎地使用多重继承
标签:style blog http color 使用 os
原文地址:http://blog.csdn.net/zhengsenlie/article/details/37754587