Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single function to pack multiple classes #1009

Open
dlandtaa opened this issue Mar 21, 2022 · 1 comment
Open

Single function to pack multiple classes #1009

dlandtaa opened this issue Mar 21, 2022 · 1 comment

Comments

@dlandtaa
Copy link

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?

@redboltz
Copy link
Contributor

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants