protobuf---messge嵌套get set

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

 

posted @ 2015-01-08 16:24  helloweworld  阅读(1093)  评论(0编辑  收藏  举报