It is right.
If you need type safety, a little template magic can help you:
template<class Msg,
std::enable_if_t<std::is_base_of<protobuf::Message,
Msg>::value>>
std::unique_ptr<Msg> clone(const Msg* msg)
{
std::unique_ptr<Msg> p(msg->New());
p->CopyFrom(*msg);
return p;
}
And you will see that I wrapped the protocol buffers object in unique_ptr. This provides some exception safety and has the added benefit of converting to shared_ptr.
? , , Foo, 2 , bar baz:
void test(const Foo* source_foo)
{
std::shared_ptr<Foo> myclone(clone(source_foo));
myclone->bar() += " cloned";
myclone->baz() += " cloned again";
auto mybar = std::shared_ptr<const std::string>(myclone, &myclone->bar());
auto mybaz = std::shared_ptr<const std::string>(myclone, &myclone->baz());
give_away(mybar);
do_something_else(mybaz);
}