I am implementing polymorphic protocol handlers and in my base class I want to have a pure virtual function where the template function parameter is unspecified. But my compiler is complaining. I guess this cannot be done. Anyone any suggestions on how to achieve this? If at all possible? Otherwise I will have to scrap the polymorphic idea.
Here is my code which gives me error C2976 ‘ProtoWrapper’ : too few template arguments. compiler=MSVC++2008.
#include "smartptrs.hpp"
#include <iostream>
class realproto {
public:
const char* getName() const { return "realproto"; }
};
class real2ndproto {
public:
const char* get2Name() const { return "real2ndproto"; }
};
template<typename T>
class ProtoWrapper : public ref_type {
public:
ProtoWrapper(T* real) : m_msg(real) {}
~ProtoWrapper() { delete m_msg; } //cannot have smart ptr on real_proto - so do this way
T* getMsg() { return m_msg; }
private:
T* m_msg;
};
class ProtocolDecoder
{
public:
virtual void Parse(ProtoWrapper<>* decoded_msg) = 0; //problem line - compile error
};
class CSTA2Decoder : public ProtocolDecoder
{
public:
virtual void Parse(ProtoWrapper<realproto>* decoded_msg) {
realproto* pr = decoded_msg->getMsg();
std::cout << pr->getName() << std::endl;
}
};
int main(int argc, char* argv[])
{
{
ref_ptr<ProtoWrapper <realproto> > msg2 = new ProtoWrapper<realproto>(new realproto);
realproto* pr1 = msg2->getMsg();
std::cout << "created new realproto\n";
}
return 0;
}
To pull this off, you need to turn
ProtocolDecoderinto a class template: