protobuf message holding reference to another message of same type

旧时模样 提交于 2019-12-11 04:17:34

问题


I have a Player structure which holds a list of pointers to its closest neighbors. The structure might look as follows in C++:

struct Player {
  string handle;
  vector<Player*> neighbors;
};

I want to use protobuf to serialize instances of this class. How would I write a message definition to represent the above structure?


回答1:


There is no concept of "reference" in protobuf.

Therefore the sanest way to do it would be to:

message Player {
  required string handle = 1;
  repeated string neighborHandles = 2;
};

Usually you would then convert them to C++ references when you are done deserializing.




回答2:


I think this would do the trick:

message Player
{
  required string handle = 1;

  repeated Player neighbors = 2;
}

I compiled the definition with protobuf-c and it seems to be working.



来源:https://stackoverflow.com/questions/17471765/protobuf-message-holding-reference-to-another-message-of-same-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!