如何在枚举中使不同的元素在C ++中的类中有不同的功能?

时间:2017-05-14 01:46:33

标签: c++ function class enums

我有enum类型roles,如下所示:

enum roles { reader, librarian, dataManager, administrator };

class Role
{
public:
    Role();
    void setRole(roles roleID);
    roles getRole();
    ~Role();
private:
    roles roleID;
};

假设每个读者都有searchForBook()seeNotifications()两个函数,每个图书馆员都有add()removeFromSystem()两个函数。 我应该怎么做才能实现上述内容,以便在完成后,代码可能是这样的(实际上,这段代码在C ++中不起作用,应该是这样的):

enum roles { reader, librarian, dataManager, administrator };

class Role
{
public:
    Role();
    void setRole(roles roleID);
    roles getRole();
    switch (roleID)
    {
    case reader: //they have two functions to be used, I mean
        searchForBook();
        seeNotifications();
    case librarian: //they have two functions to be used, I mean
        add();
        removeFromSystem();
    }
    ~Role();
private:
    roles roleID;
};

0 个答案:

没有答案