[TOC]

概述

文章参考:https://tennysonsky.blog.csdn.net/article/details/73921025

限定修饰符说明

repeated 代表可重复,我们可以理解为数组:

1
2
3
4
5
6
7
8
9
10
11
12
13
syntax = "proto3";//指定版本信息,不指定会报错

message Person //message为关键字,作用为定义一种消息类型
{
string name = 1; //姓名
int32 id = 2; //id
string email = 3; //邮件
}

message AddressBook
{
repeated Person people = 1;
}

字段API

而对于字段修饰符为repeated的字段生成的函数,则稍微有一些不同,如people字段,则编译器会为其产生如下的代码:

1
2
3
4
5
6
7
int people_size() const;
void clear_people();
const ::Person& people(int index) const;
::Person* mutable_people(int index);
::Person* add_people();
::google::protobuf::RepeatedPtrField< ::Person >* mutable_people();
const ::google::protobuf::RepeatedPtrField< ::Person >& people() const;