使用dymaic_cast typeid 虚函数 对operator ==进行重载
//Animal.h #pragma once #include<string> using std::string; namespace Marco { class Animal { friend bool operator ==(const Animal& lhs, const Animal& rhs); public: Animal(const string& kind); Animal(const Animal& an); Animal& operator =(const Animal& an); Animal(Animal&&)noexcept; Animal& operator =(Animal&&)noexcept; virtual ~Animal(){} virtual bool equal(const Animal&)const; string kind; }; } //Reccoon.h #pragma once #include<string> #include"Animal.h" using std::string; namespace Marco { class Reccoon :virtual public Animal { public: Reccoon(const string& s); Reccoon(const Reccoon&); Reccoon(Reccoon&&)noexcept; Reccoon& operator=(const Reccoon&); Reccoon& operator=(Reccoon&&)noexcept; virtual bool equal(const Animal&)const; string kind; }; } //Bear.h #pragma once #include<string> #include"Animal.h" using std::string; namespace Marco { class Bear:virtual public Animal { public: Bear(const string& kind); Bear(const Bear& be); Bear(Bear&&)noexcept; Bear& operator =(const Bear&); Bear& operator =(Bear&&)noexcept; virtual bool equal(const Animal& rhs)const; string kind; }; } //Animal.cpp #include"Animal.h" using namespace Marco; Animal::Animal(const string& s):kind(s){} Animal::Animal(const Animal& an):kind(an.kind) { } Animal& Animal::operator =(const Animal& an) { kind = an.kind; return *this; } Animal::Animal(Animal&& an)noexcept:kind(std::move(an.kind)) { } Animal& Animal::operator =(Animal&& an)noexcept { kind = an.kind; return *this; } bool Marco::operator ==(const Animal& lhs,const Animal& rhs) { return typeid(lhs) == typeid(rhs) && lhs.equal(rhs); } bool Animal::equal(const Animal& rhs)const { return kind == rhs.kind; } //Reccoon.h #include"Reccoon.h" using namespace Marco; Reccoon::Reccoon(const string& s):Animal(s),kind(s){} Reccoon::Reccoon(const Reccoon& re):Animal(re),kind(re.kind) { } Reccoon::Reccoon(Reccoon&& re)noexcept:Animal(std::move(re)),kind(std::move(re.kind)) { } Reccoon& Reccoon::operator =(const Reccoon& re) { Animal::operator =(std::move(re)); kind = re.kind; return *this; } Reccoon& Reccoon::operator =(Reccoon&& re)noexcept { Animal::operator =(std::move(re)); kind = std::move(re.kind); return *this; } bool Reccoon::equal(const Animal& rhs)const { auto r = dynamic_cast<const Reccoon&>(rhs); return kind == r.kind && Animal(rhs).equal(*this); } //Bear.cpp #include"Bear.h" using namespace Marco; Bear::Bear(const string& s):Animal(s),kind(s){} Bear::Bear(const Bear& be):Animal(be),kind(be.kind){} Bear::Bear(Bear&& be)noexcept:Animal(std::move(be)),kind(std::move(be.kind)){} Bear& Bear::operator =(const Bear& be) { Animal::operator =(be); kind = be.kind; return *this; } Bear& Bear::operator =(Bear&& be)noexcept { Animal::operator =(std::move(be)); kind = std::move(be.kind); return *this; } bool Bear::equal(const Animal& rhs)const { auto r = dynamic_cast<const Bear&>(rhs); return kind == r.kind && Animal(rhs).equal(*this); } //main.cpp #include<iostream> #include"panda.h" using namespace std; using namespace Marco; int main() { Panda p1("Marco"); Panda p2("Marco"); Bear p3("Panda"); Bear p4("Slip"); Bear p5("Panda"); p1 == p2 ? cout << "equal" << endl : cout << "unequal" << endl; p1 == p3 ? cout << "equal" << endl : cout << "unequal" << endl; p3 == p4 ? cout << "equal" << endl : cout << "unequal" << endl; p3 == p5 ? cout << "equal" << endl : cout << "unequal" << endl; system("pause"); return 0; }