条款40:明智而审慎的使用多重继承
使用private继承 实现:
#pragma once #include "IPerson.h" #include "PersonInfo.h" class CPerson1 : public IPerson, private PersonInfo { public: explicit CPerson1(const char* name) : PersonInfo(name) { } virtual ~CPerson1() { } virtual std::string name() const { return PersonInfo::theName(); } virtual std::string birthDate() const { return PersonInfo::theBirthDate(); } private: virtual const char* valueDelimOpen() const { return ""; } virtual const char* valueDelimClose() const { return ""; } }; //--------------------------- #pragma once #include <string> class IPerson{ public: virtual ~IPerson(){ } virtual std::string name() const = 0; virtual std::string birthDate() const = 0; }; //---------------------------- #pragma once #include <string> class PersonInfo { const char* name; public: explicit PersonInfo(const char* name) : name(name) { } virtual ~PersonInfo() { } virtual const char* theName() const { static char value[255]; std::strcpy(value, valueDelimOpen()); std::strcat(value, name); std::strcat(value, valueDelimClose()); return value; } virtual const char* theBirthDate() const { return ""; } virtual const char* valueDelimOpen() const { return "["; } virtual const char* valueDelimClose() const { return "]"; } }; //--------------------------- #include <iostream> #include "Person1.h" #include "Person2.h" #include "Person3.h" using namespace std; int main(){ //debug 探测内存泄露 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); //int* p = new int; CPerson1 p1("Lee Psalm"); cout << p1.name() << endl; cout << p1.name() << endl; CPerson2 p2("Lee Psalm 2"); cout << p2.name() << endl; cout << p2.name() << endl; CPerson3 p3("Lee Psalm 3"); cout << p3.name() << endl; cout << p3.name() << endl; return 0; }
使用 组合 实现:
#pragma once #include <string> class IPerson{ public: virtual ~IPerson(){ } virtual std::string name() const = 0; virtual std::string birthDate() const = 0; }; //------------------------------ #pragma once #include <string> class PersonInfo { const char* name; public: explicit PersonInfo(const char* name) : name(name) { } virtual ~PersonInfo() { } virtual const char* theName() const { static char value[255]; std::strcpy(value, valueDelimOpen()); std::strcat(value, name); std::strcat(value, valueDelimClose()); return value; } virtual const char* theBirthDate() const { return ""; } virtual const char* valueDelimOpen() const { return "["; } virtual const char* valueDelimClose() const { return "]"; } }; //------------------------------ #pragma once #include "IPerson.h" #include "PersonInfo.h" #include <memory> class CPerson3 : public IPerson { private: class PersonInfo2 : public PersonInfo { public: explicit PersonInfo2(const char* name) : PersonInfo(name) { } virtual const char* valueDelimOpen() const { return "<"; } virtual const char* valueDelimClose() const { return ">"; } }; std::shared_ptr<PersonInfo2> pPersonInfo; //PersonInfo2* pPersonInfo; public: CPerson3(const char* name) : pPersonInfo(new PersonInfo2(name)) { } virtual ~CPerson3() { } virtual std::string name() const { return pPersonInfo->theName(); } virtual std::string birthDate() const { return pPersonInfo->theBirthDate(); } }; //------------------------------ //------------------------------ #include <iostream> #include "Person1.h" #include "Person2.h" #include "Person3.h" using namespace std; int main(){ //debug 探测内存泄露 _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF); //int* p = new int; CPerson1 p1("Lee Psalm"); cout << p1.name() << endl; cout << p1.name() << endl; CPerson2 p2("Lee Psalm 2"); cout << p2.name() << endl; cout << p2.name() << endl; CPerson3 p3("Lee Psalm 3"); cout << p3.name() << endl; cout << p3.name() << endl; return 0; } //------------------------------
**
常记溪亭日暮,沉醉不知归路。兴尽晚回舟,误入藕花深处。争渡,争渡,惊起一滩鸥鹭。
昨夜雨疏风骤,浓睡不消残酒。试问卷帘人,却道海棠依旧。知否?知否?应是绿肥红瘦。