都可以用来获取协议对象序列化成字节流的长度。
GetCachedSize()取的是调用ByteSize()之后保存的值。
如果调用GetCachedSize()时还未调用过ByteSize(),返回0。
#include "common.pb.h"
#include <iostream>
int main()
{
TestProtocol one;
one.set_data1(1);
one.set_data2(2);
int size1 = one.GetCachedSize(); // 0
int size2 = one.ByteSize(); // 4
int size3 = one.GetCachedSize(); // 4
std::cout << size1 << " " << size2 << " " << size3 << std::endl;
return 0;
}
输出为:0 4 4