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 @   helloweworld  阅读(1117)  评论(0编辑  收藏  举报
编辑推荐:
· 软件产品开发中常见的10个问题及处理方法
· .NET 原生驾驭 AI 新基建实战系列:向量数据库的应用与畅想
· 从问题排查到源码分析:ActiveMQ消费端频繁日志刷屏的秘密
· 一次Java后端服务间歇性响应慢的问题排查记录
· dotnet 源代码生成器分析器入门
阅读排行:
· ThreeJs-16智慧城市项目(重磅以及未来发展ai)
· .NET 原生驾驭 AI 新基建实战系列(一):向量数据库的应用与畅想
· 软件产品开发中常见的10个问题及处理方法
· Vite CVE-2025-30208 安全漏洞
· MQ 如何保证数据一致性?
点击右上角即可分享
微信分享提示