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?

.proto

syntax = "proto2";

package test;

message Msg1
{
required int32 x = 1;
}

message Msg2
{
required int32 y = 1;
}

test.cpp

#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;
}

-- 
You received this message because you are subscribed to the Google Groups 
"Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/protobuf.
For more options, visit https://groups.google.com/d/optout.

Reply via email to