#include<functional> // reference_wrapper#include<iostream>#include<string>#include<vector>// Since Doctor and Patient have a circular dependency, we're going to forward declare PatientclassPatient;classDoctor{private:std::stringm_name{};std::vector<std::reference_wrapper<constPatient>>m_patient{};public:Doctor(conststd::string&name):m_name{name}{}voidaddPatient(Patient&patient);// We'll implement this function below Patient since we need Patient to be defined at that pointfriendstd::ostream&operator<<(std::ostream&out,constDoctor&doctor);conststd::string&getName()const{returnm_name;}};classPatient{private:std::stringm_name{};std::vector<std::reference_wrapper<constDoctor>>m_doctor{};// so that we can use it here// We're going to make addDoctor private because we don't want the public to use it.// They should use Doctor::addPatient() instead, which is publicly exposedvoidaddDoctor(constDoctor&doctor){m_doctor.push_back(doctor);}public:Patient(conststd::string&name):m_name{name}{}// We'll implement this function below to parallel operator<<(std::ostream&, const Doctor&)friendstd::ostream&operator<<(std::ostream&out,constPatient&patient);conststd::string&getName()const{returnm_name;}// We'll friend Doctor::addPatient() so it can access the private function Patient::addDoctor()friendvoidDoctor::addPatient(Patient&patient);};voidDoctor::addPatient(Patient&patient){// Our doctor will add this patientm_patient.push_back(patient);// and the patient will also add this doctorpatient.addDoctor(*this);}std::ostream&operator<<(std::ostream&out,constDoctor&doctor){if(doctor.m_patient.empty()){out<<doctor.m_name<<" has no patients right now";returnout;}out<<doctor.m_name<<" is seeing patients: ";for(constauto&patient:doctor.m_patient)out<<patient.get().getName()<<' ';returnout;}std::ostream&operator<<(std::ostream&out,constPatient&patient){if(patient.m_doctor.empty()){out<<patient.getName()<<" has no doctors right now";returnout;}out<<patient.m_name<<" is seeing doctors: ";for(constauto&doctor:patient.m_doctor)out<<doctor.get().getName()<<' ';returnout;}intmain(){// Create a Patient outside the scope of the DoctorPatientdave{"Dave"};Patientfrank{"Frank"};Patientbetsy{"Betsy"};Doctorjames{"James"};Doctorscott{"Scott"};james.addPatient(dave);scott.addPatient(dave);scott.addPatient(betsy);std::cout<<james<<'\n';std::cout<<scott<<'\n';std::cout<<dave<<'\n';std::cout<<frank<<'\n';std::cout<<betsy<<'\n';return0;}
程序运行结果:
12345
James is seeing patients: Dave
Scott is seeing patients: Dave Betsy
Dave is seeing doctors: James Scott
Frank has no doctors right now
Betsy is seeing doctors: Scott
#include<iostream>#include<string>classCar{private:std::stringm_name;intm_id;public:Car(conststd::string&name,intid):m_name{name},m_id{id}{}conststd::string&getName()const{returnm_name;}intgetId()const{returnm_id;}};// Our CarLot is essentially just a static array of Cars and a lookup function to retrieve them.// Because it's static, we don't need to allocate an object of type CarLot to use itclassCarLot{private:staticCars_carLot[4];public:CarLot()=delete;// Ensure we don't try to create a CarLotstaticCar*getCar(intid){for(intcount{0};count<4;++count){if(s_carLot[count].getId()==id){return&(s_carLot[count]);}}returnnullptr;}};CarCarLot::s_carLot[4]{{"Prius",4},{"Corolla",17},{"Accord",84},{"Matrix",62}};classDriver{private:std::stringm_name;intm_carId;// we're associated with the Car by ID rather than pointerpublic:Driver(conststd::string&name,intcarId):m_name{name},m_carId{carId}{}conststd::string&getName()const{returnm_name;}intgetCarId()const{returnm_carId;}};intmain(){Driverd{"Franz",17};// Franz is driving the car with ID 17Car*car{CarLot::getCar(d.getCarId())};// Get that car from the car lotif(car)std::cout<<d.getName()<<" is driving a "<<car->getName()<<'\n';elsestd::cout<<d.getName()<<" couldn't find his car\n";return0;}