It's probably something trivial but I can't figure it out :( I have 2 messages that have similar structure and when I try to encode message #1 and decode it as message #2 it works just fine. Why??? If that's how it is supposed to work how to distinguish between them? Please advise. Self contained fest code is below. Another probably very close related question: if message in .proto file is empty (does not contain any fields) it is serialised into zero byte long data. Is this how it is supposed to be? Than how to send such a message?
#include <string>
#include <iostream>
#ifdef _DEBUG
#pragma comment(lib,"./protobuf-3.2.0/lib/libprotobufd.lib")
#pragma comment(lib,"./protobuf-3.2.0/lib/libprotocd.lib")
#else
#pragma comment(lib,"./protobuf-3.2.0/lib/libprotobuf.lib")
#pragma comment(lib,"./protobuf-3.2.0/lib/libprotoc.lib")
#endif
#include ".pb.h"
int main(int argc, char* argv[])
{
std::string str;
test::Msg1 msg1; msg1.set_x(1);
if (!msg1.SerializeToString(&str))
{
std::cerr << "failed to serialise packet" << std::endl;
return -1;
}
test::Msg2 msg2;
if (!msg2.ParseFromString(str))
{
std::cerr << "failed to parse packet" << std::endl;
return -1;
}
std::cerr << "UNEXPECTED: type Msg1 was successfully parsed as type Msg2, why ???" << std::endl;
return 0;
}