msgpack-c icon indicating copy to clipboard operation
msgpack-c copied to clipboard

Single function to pack multiple classes

Open dlandtaa opened this issue 3 years ago • 1 comments

I would like to have a base class that handles the creation of the messagepack so that all the derived classes can use it.

class BaseClass
{
public:
    msgpack::sbuffer Pack()
    {
        msgpack::sbuffer buff;
        msgpack::pack(buff, *this);
        return buff;
    }
};

class DerivedClass: public BaseClass
{
public:
    MSGPACK_DEFINE_MAP(message);

private:
    std::string message;
};

That way I can do

DerivedClass d;
d.Pack();

I will have several classes all deriving from BaseClass so it would be best to have Pack() in that class. However, when I attempt to do this I get this error:

error: no member named 'msgpack_pack' in 'BaseClass'

Is it possible to put just the call to msgpack::pack in a single function that all the types can use?

dlandtaa avatar Mar 21 '22 02:03 dlandtaa

It is not possible. You need to implementt your custom paking logic using virtual function mechanism by yourself.

redboltz avatar Mar 21 '22 02:03 redboltz