繁体   English   中英

如何在C ++中检查向量中的哪些对象

[英]How to check which objects in vector in C++

那么, Hospital是一个有病人载体的班级。 FemaleInFemaleOutMaleInMaleOut是患者的派生类(基础类)。 那些类有toString函数(方法)。 我所试图做的是,在display的方法, Hospital类,我只是想只有在的情况下,显示Outpatient是父类的FemaleOutMaleoutInpatient这是父类的FemaleInMaleIn 从我的想法只调用特定的方法,例如, Outpatient ,我将不得不自动知道哪个对象在哪个矢量索引。 是否有任何想法只能拨打toString特定的类,例如, FemaleInMaleIn这里父类是Inpatient 感谢您的任何帮助或建议。

void Hospital::determinePatientType()
{
    int selection;
    cout << endl;
    cout << "What is the patient type?" << endl;
    cout << "1. Female Inpatient" << endl;
    cout << "2. Female Outpatient" << endl;
    cout << "3. Male Inpatient" << endl;
    cout << "4. Male Outpatient" << endl;
    cout << endl;
    cin >> selection;

    switch(selection)
    {
    case 1:
        patients.push_back(new FemaleIn());
        cout << patients.back() << endl;
        patients[totalPatients]->enterPatientData();
        totalPatients++;
        break;
    case 2:
        patients.push_back(new FemaleOut());
        cout << patients.back() << endl;
        patients[totalPatients]->enterPatientData();
        totalPatients++;
        break;
    case 3:
        patients.push_back(new MaleIn());
        cout << patients.back() << endl;
        patients[totalPatients]->enterPatientData();
        totalPatients++;
        break;
    case 4:
        patients.push_back(new MaleOut());
        cout << patients.back() << endl;
        patients[totalPatients]->enterPatientData();
        totalPatients++;
        break;
    default:
        return;
    }

}

void Hospital::display(string type)
{


        cout << "Patient Name     Spouse Name    Sex    Patient Type    Unit/Appt. Date    Diagnosis" << endl;
        cout << "===================================================================================" << endl;

        if(type=="All")
        {
            for(int i=0;i<patients.size();i++)
            {
                patients[i]->toString();
            }

        }
        else if(type=="Outpatient")
        {
            for(int i=0;i<patients.size();i++)
            {
                patients[i]->toString();
            }
        }
        else
        {
            for(int i=0;i<patients.size();i++)
            {
                patients[i]->toString();
            }
        }

}

我认为这个问题可能类似于如何检查对象的类型是否是C ++中的特定子类?

我建议像:

Class Patient{
    virtual bool display(string filter);
};
Class OutPatient : Patient {
    bool display(string filter) override;
};
bool OutPatient::display(string filter) {
    if (filter != "OutPatient")
        return false;
    //Do stuff
    return true;
}
Class InPatient : Patient {
    bool display(string filter) override;
};
// You could just make this the default definition on display on Patient
bool InPatient::display(string filter) {
    return false;
}

接着:

void Hospital::display(string type)
{
    for(auto& patient: patients)
        patient->display(type);
}

以快速和脏的方式,您可以使用dynamic_cast到某个类指针来检测给定的实例是否属于该类:

    if (type == "Outpatient")
    {
        for(int i=0; i<patients.size(); ++i)
        {
            // try to cast parent class pointer to one of child class
            // if one is pointer to that child class p is not nullptr
            // otherwise p is nullptr
            Outpatient * p = dynamic_cast<Outpatient *>(patients[i]);

            if (p) {
                p->toString();
            }
        }
    }

对于干净的方式,您可以使用访客模式

访客模式实施:

#include <iostream>
#include <vector>
#include <string>
#include <functional>
#include <utility>

class AbstractDirectionPatientsDispatcher;
class AbstractGenderPatientDispatcher;

class Patient
{
public:
    virtual void accept(AbstractDirectionPatientsDispatcher &dispatcher) = 0;
    virtual void accept(AbstractGenderPatientDispatcher &dispatcher) = 0;

    std::string name;
};

class InPatient;
class OutPatient;

class AbstractDirectionPatientsDispatcher {
public:
    virtual void dispatch(InPatient &patient) = 0;
    virtual void dispatch(OutPatient &patient) = 0;
};

class FemalePatient;
class MalePatient;

class AbstractGenderPatientDispatcher {
public:
    virtual void dispatch(FemalePatient &patient) = 0;
    virtual void dispatch(MalePatient &patient) = 0;
};

template <typename PatientClass, typename Dispatcher>
class CRTPDispatchApplier : virtual public Patient
{
public:
    void accept(Dispatcher &dispatcher) override {
        dispatcher.dispatch(static_cast<PatientClass &>(*this));
    }
};

class InPatient : public CRTPDispatchApplier<InPatient, AbstractDirectionPatientsDispatcher>
{
};

class OutPatient : public CRTPDispatchApplier<OutPatient, AbstractDirectionPatientsDispatcher>
{
};

class FemalePatient : public CRTPDispatchApplier<FemalePatient, AbstractGenderPatientDispatcher>
{
};

class MalePatient : public CRTPDispatchApplier<MalePatient, AbstractGenderPatientDispatcher>
{
};


class InFemale : public FemalePatient, public InPatient
{
};

class OutFemale : public FemalePatient, public OutPatient
{
};

class InMale : public MalePatient, public InPatient
{
};

class OutMale : public MalePatient, public OutPatient
{
};

class DummyDirectionDispatecher : public AbstractDirectionPatientsDispatcher
{
public:
    void dispatch(InPatient & ) override {
    }

    void dispatch(OutPatient & ) override {
    }
};

class DummyGenderDispatcher : public AbstractGenderPatientDispatcher
{
public:
    void dispatch(FemalePatient &) override {
    }

    void dispatch(MalePatient &) override {
    }
};

template <typename Direction>
class DispatchByDirection : public DummyDirectionDispatecher
{
public:
    DispatchByDirection(std::function<void(Direction &)> action) :
        m_action(std::move(action))
    {}

    void dispatch(Direction & p) override {
        m_action(p);
    }

private:
    std::function<void(Direction &)> m_action;
};

template <typename Gender>
class DispatchByGender : public DummyGenderDispatcher
{
public:
    DispatchByGender(std::function<void(Gender &)> action) :
        m_action(std::move(action))
    {}

    void dispatch(Gender & p) override {
        m_action(p);
    }

private:
    std::function<void(Gender &)> m_action;
};

int main() {
    InFemale f1;
    OutFemale f2;
    InMale m1;
    OutMale m2;
    f1.name = "Eve";
    f2.name = "Alice";
    m1.name = "Bob";
    m2.name = "Charlie";

    std::vector<Patient *> patients;
    patients.push_back(&f1);
    patients.push_back(&f2);
    patients.push_back(&m1);
    patients.push_back(&m2);

    DispatchByDirection<InPatient> print_in_patients{[](InPatient &patient){
        std::cout << "in: " << patient.name << std::endl;
    }};

    for (auto p : patients) {
        p->accept(print_in_patients);
    }
    std::cout << std::endl;

    DispatchByDirection<OutPatient> print_out_patients{[](OutPatient &patient) {
        std::cout << "out: " << patient.name << std::endl;
    }};

    for (auto p : patients) {
        p->accept(print_out_patients);
    }
    std::cout << std::endl;

    DispatchByGender<FemalePatient> print_female{[](FemalePatient &patient) {
        std::cout << "female: " << patient.name << std::endl;
    }};

    for (auto p : patients) {
        p->accept(print_female);
    }
    std::cout << std::endl;

    DispatchByGender<MalePatient> print_male{[](MalePatient &patient) {
        std::cout << "male: " << patient.name << std::endl;
    }};

    for (auto p : patients) {
        p->accept(print_male);
    }
    std::cout << std::endl;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM