枚举类在其他类中的设置和获取方法

时间:2018-06-22 17:30:56

标签: c++ enums polymorphism shared-ptr

我有基类Employee和派生类concreteEmployee

class Employee {
    virtual void &getStatus() {}  ??
}

class concreteEmployee : public Employee {

public:
enum class Status {
    Intern,
    Worker,
    Manager
};

Status status;

void setStatus() {
    ?????
}


... &getStatus() { // virtual from Employee
    return Status; ???
} 

我的指针向量为vector<shared_ptr<Employee>> Firm;,我通过引用sourceEmployee传递。 我想做的是:

  1. 在雇用“ Worker”之类的新员工时设置具体身份。

    void hireEmployee(vector<shared_ptr<Employee>>& sourceEmployee) {
    sourceEmployee.emplace_back(new concreteEmployee(fillName, fillSurname));
    sourceEmployee.back()->status = ???
    

    我想这样做,而不是在std::string Status = "Worker"等中使用concreteEmployee字段。 和经典的设置器/获取器。

  2. 我想从concreteEmployee类中获取Employee的状态并将其打印在某处。我有矢量派生对象(使用std::shared_ptr),并且想要得到这样的状态?

    void employeeShowcase(const vector<shared_ptr<Employee>>& sourceEmployee) {
        cout << sourceEmployee[index]->getStatus()
        ... // output should be like i.e "Worker"
    }
    

1 个答案:

答案 0 :(得分:1)

如果班级的消费者应该可以看到您的Employee状态,则表明您必须在Employee而不是ConcreteEmployee的范围内定义该枚举