package test_namespace;message ChildMsg {optional string child = 1; }message FatherMsg {optional string father = 1; optional ChildMsg child_msg = 2; }
或者
message FatherMsg {optional string father = 1; message ChildMsg {optional string child = 1;}optional ChildMsg child_msg = 2; }
上述两种生成的test.pb.h不同, 但get set一样
#include <stdio.h> #include <iostream> #include <string>#include "test.pb.h"using namespace std;int main() {test_namespace::FatherMsg father_msg;father_msg.set_father("fathermsg"); father_msg.mutable_child_msg()->set_child("childmsg");cout << father_msg.has_father() << endl;cout << father_msg.has_child_msg() << endl;cout << father_msg.child_msg().has_child() << endl;cout << father_msg.father() << endl; cout << father_msg.child_msg().child() << endl;return 0; }