在 C++ 中重载子类中的枚举
Overloading an enum in a child class in c++
我有兴趣定义一个要继承的通用类,该类根据枚举和该枚举到某些数据结构的映射以不同的方式操作。
在下面的代码中,我test_child继承自test_parent,其中已经实现了共享函数。我的计划是让许多类继承自像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
不确定这是你想要的,但使用模板,你可能会这样做
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}}
};
演示
嗨,
我想我可能在以下示例中找到了答案:
#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;
}
我对待枚举值的地方就像它们最初设想的那样,ints!
相关文章:
- 枚举类'classname'的多重定义
- typedef 枚举和枚举类有什么区别?
- 在 qml 中使用 Q_ENUM 和 Q_PROPERTY 作为枚举类
- 为什么 int 类型的枚举类值不能用作 int
- 在 C++ 中输出枚举类类型的向量元素
- 我在 .h 中有一个枚举类,并且在.cpp错误中有一个运算符重载:与"运算符<<不匹配
- 为什么不能直接引用作用域枚举类成员,而不能为无作用域枚举生成类成员?
- 如何为底层类型 int 的枚举专门化类
- 枚举、类、命名空间和长名称
- 一种创建将字符串常量返回给枚举的类的廉价方法,反之亦然
- 在 C++ 中重载子类中的枚举
- 同名的枚举和类成员函数
- 具有自定义枚举C++类
- 初始值使用什么?结构、枚举或类,#defines c++
- 关于 c++ 中的枚举和类以及它们的工作原理
- 如何将基类上的枚举传递给子类的构造函数
- 实现同时具有枚举和类行为的东西
- 枚举和类——运行时错误!
- 枚举子窗口不起作用,但枚举Windows可以...?
- c++枚举的类实现