【问题标题】:setters and getters for enum class inside other class其他类中枚举类的设置器和获取器
【发布时间】:2018-06-22 17:30:55
【问题描述】:

我有基类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 = ???
    

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

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

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

【问题讨论】:

    标签: c++ enums polymorphism shared-ptr


    【解决方案1】:

    如果你的 Employee 状态应该对你的类的消费者可见,那么这意味着你必须在 Employee 的范围内定义该枚举,而不是 ConcreteEmployee

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-19
      • 2020-05-17
      • 1970-01-01
      • 2017-01-14
      • 2022-08-19
      • 2013-04-28
      相关资源
      最近更新 更多