[英]Template class extending from non-template base class and overriding function parameters
I am trying to create a non-template base class for the PacketDecoder so I can store Base* in a std::map. 我正在尝试为PacketDecoder创建一个非模板基类,因此我可以将Base *存储在std :: map中。 The decode function is being overridden properly.
解码功能正在被正确覆盖。 The encode function is giving me this error "'encode' is not virtual and cannot be declared pure".
编码功能给我这个错误“'编码'不是虚拟的,不能声明为纯”。 I might be thinking about this problem wrong coming from a Java background.
我可能会考虑来自Java背景的这个问题。 Is there a more idiomatic c++ approach to this problem?
是否有更惯用的c ++方法解决这个问题?
class Base {
public:
virtual Packet* decode(folly::io::Cursor& cursor) = 0;
virtual void encode(Packet* packet) = 0;
};
template<typename T, typename std::enable_if<std::is_base_of<Packet, T>::value>::type* = nullptr>
class PacketDecoder : public Base {
public:
virtual T* decode(folly::io::Cursor& cursor) = 0;
virtual void encode(T *packet) = 0;
};
Example usage: 用法示例:
class TestDecoder : public PacketDecoder<ProxyJoinPacket> {
public:
ProxyJoinPacket *decode(folly::io::Cursor &cursor) override {
uint32_t stringLength;
if (!cursor.tryReadBE<uint32_t>(stringLength)) {
throw std::runtime_error("Failed to read string length");
}
if (cursor.length() < stringLength) {
throw std::runtime_error("Too few bytes for string");
}
auto uuid = cursor.readFixedString(stringLength);
return new ProxyJoinPacket(uuid);
}
void encode(ProxyJoinPacket *packet) override {
}
};
std::vector<Base*> decoders;
void a() {
decoders.insert(new TestDecoder()); // error "Allocating an object of abstract class type 'TestDecoder'"
}
The issue is that your original class has this virtual function: 问题是你的原始类有这个虚函数:
virtual void encode(Packet* packet) = 0;
Then your inherited class has this: 然后你继承的类有这个:
virtual void encode(T *packet) = 0;
That's two different virtual functions. 这是两个不同的虚拟功能。
So when you do: 所以当你这样做时:
void encode(ProxyJoinPacket *packet) override {
You only override the method int he inherited class, not in Base
, which is still missing. 您只覆盖继承类的方法int,而不是
Base
,它仍然缺失。
So change your inherited class to use Packet
因此,更改继承的类以使用
Packet
virtual void encode(Packet* packet) = 0;
You can also change your inherited class and add something like: 您还可以更改继承的类并添加如下内容:
void encode(Packet* packet) final {
encode(dynamic_cast<T*> packet);
}
Don't forget the virtual destructors as well... 不要忘记虚拟析构函数......
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.