在C ++的子类中重载枚举

时间:2019-01-24 22:20:45

标签: c++ c++11

我对定义一个要继承的通用类感兴趣,该通用类基于枚举和该枚举到某些数据结构的映射而有所不同。

在下面的代码中,我具有从test_parent继承的test_child,该对象具有已实现的共享功能。我的计划是让许多类从类似parent_class的类继承,但要定义一个唯一的“字段”枚举和相应的“映射”映射。

#include <iostream>
#include <string>
#include <unordered_map>

class test_parent {
public:
    enum class field {
        A,
        B,
        C
    };

    typedef struct {
        std::string s;
        int i, j;
    } data_t;

    std::unordered_map<field, data_t> mapping {
        {field::A, {"A", 1, 1}},
        {field::B, {"B", 2, 2}},
        {field::C, {"C", 3, 3}}
    };

    int get_i (field f) {
        return mapping[f].i;
    }

    std::string get_s (field f) {
        return mapping[f].s;
    }   
};

class test_child : test_parent {
public:
    enum class field {
        D,
        E
    };

    std::unordered_map<field, data_t> mapping {
        {field::D, {"D", 4, 4}},
        {field::E, {"E", 5, 5}}
    };
};

int main () {
    test_parent tp;
    test_child tc;

    std::cout << tp.get_i(test_parent::field::A) << " " << tc.get_i(test_child::field::E) << std::endl;

    return 0;
}

此代码返回编译错误:

test.cpp: In function ‘int main()’:
test.cpp:55:86: error: no matching function for call to ‘test_child::get_i(test_child::field)’
std::cout << tp.get_i(test_parent::field::A) << " " << tc.get_i(test_child::field::E) << std::endl;
                                                                                    ^
test.cpp:28:6: note: candidate: int test_parent::get_i(test_parent::field)
int get_i (field f) {
    ^~~~~
test.cpp:28:6: note:   no known conversion for argument 1 from ‘test_child::field’ to ‘test_parent::field’

但是我希望打印的是:

1 5

2 个答案:

答案 0 :(得分:0)

不确定是不是想要的,但是可以使用模板

struct data_t
{
    std::string s;
    int i;
    int j;
};

template <typename E>
class test_parent {
public:
    int get_i(E e) const { return mapping.at(e).i; }
    const std::string& get_s(E e) const { return mapping.at(e).s; }

    static const std::unordered_map<E, data_t> mapping;
};

然后

enum class field_ABC{ A, B, C };
enum class field_DE{ D, E };

template <>
const std::unordered_map<field_ABC , data_t> test_parent<field_ABC >::mapping =  {
    {field_ABC::A, {"A", 1, 1}},
    {field_ABC::B, {"B", 2, 2}},
    {field_ABC::C, {"C", 3, 3}}
};

template <>
const std::unordered_map<field_DE, data_t> test_parent<field_DE>::mapping =  {
    {field_DE::D, {"D", 4, 4}},
    {field_DE::E, {"E", 5, 5}}
};

Demo

答案 1 :(得分:0)

嗨,我想我可能在以下示例中找到了答案:

#include <iostream>
#include <string>
#include <unordered_map>

class test_parent {
public:
    enum class field {
        A,
        B,
        C
    };

    typedef struct {
        std::string s;
        int i, j;
    } data_t;

    std::unordered_map<uint, data_t> mapping {
        {(uint) field::A, {"A", 1, 1}},
        {(uint) field::B, {"B", 2, 2}},
        {(uint) field::C, {"C", 3, 3}}
    };

    int get_i (uint f) {
        return mapping[f].i;
    }

    std::string get_s (uint f) {
        return mapping[f].s;
    }   
};

class test_child : public test_parent {
public:
    test_child () {
        test_parent::mapping = mapping;
    }

    enum class field {
        D = 4,
        E = 5
    };

    std::unordered_map<uint, data_t> mapping {
        {(uint) field::E, {"E", 5, 5}},
        {(uint) field::D, {"D", 4, 4}}
    };
};

int main () {
    test_parent tp;
    test_child tc;

    std::cout << tp.get_i((uint) test_parent::field::A) << " " << tc.get_i((uint) test_child::field::E) << std::endl;

    return 0;
}

在我将枚举值最初视为整数的情况下,它是整数!