I'm creating a library that encompasses the parsing of protobuf messages. As the library cannot know for which messages it will be used, it parses messages using a google::protobuf::DynamicMessageFactory
.
However, I would like to be able to pass the parsed messages back to other parts of the program which know about the actual message types.
Same question in other words: I have a const google::protobuf Message*
created via google::protobuf::compiler::Importer
and google::protobuf::DynamicMessageFactory
. After parsing, I know for a fact that the message is actually a TestMessage
(and the program knows about the class), can I convert the Message*
into a TestMessage
somehow?
I cannot simply dynamic_cast
the message, the result of the cast is always NULL
if the message was parsed with the general Message::ParseFromXXX
which I'm guessing is because those methods just return some kind of proxy objects and cannot know about the actual message classes.
I've also tried the following:
//message is a const google::protobuf::Message* parsed using DynamicMessageFactory
TestMessage *tr = new TestMessage;
tr->CopyFrom( *message );
but that fails with protobuf saying "trying to merge messages of different types". Note that tr->GetTypeName()
and message->GetTypeName()
do return the same type name.
I'm thankful for any help
Does TestMessage actually inherit from Message? The fact that dynamic_cast returns null suggests that it doesn't, which means that the classes are unrelated, and it doesn't make sense trying to cast from one to the other. Or maybe it uses private inheritance.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.