模板类从非模板基类扩展并覆盖函数参数

JJa*_*mes 5 c++ c++17

我正在尝试为PacketDecoder创建一个非模板基类,因此我可以将Base*存储在std :: map中.解码功能正在被正确覆盖.编码功能给我这个错误"'编码'不是虚拟的,不能声明为纯".我可能会考虑来自Java背景的这个问题.是否有更惯用的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;
};
Run Code Online (Sandbox Code Playgroud)

用法示例:

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'"
}
Run Code Online (Sandbox Code Playgroud)

Mat*_*her 2

问题是你的原始类有这个虚函数:

virtual void encode(Packet* packet) = 0;
Run Code Online (Sandbox Code Playgroud)

那么你继承的类有这样的:

virtual void encode(T *packet) = 0;
Run Code Online (Sandbox Code Playgroud)

这是两个不同的虚拟函数。

所以当你这样做时:

void encode(ProxyJoinPacket *packet) override {
Run Code Online (Sandbox Code Playgroud)

您只能重写方法 int 他继承的类,而不是 in Base,后者仍然缺失。

所以改变你继承的类来使用Packet

virtual void encode(Packet* packet) = 0;
Run Code Online (Sandbox Code Playgroud)

您还可以更改继承的类并添加如下内容:

void encode(Packet* packet) final {
    encode(dynamic_cast<T*> packet);
}
Run Code Online (Sandbox Code Playgroud)

不要忘记虚拟析构函数......