1, 下載compiler和源代碼,或直接下bin包
http://code.google.com/p/protobuf/downloads/
build protobuf: ui
2, 建立一個addressbook.proto google
3, 生成C++的stub spa
運行上面的命令將生成addressbook.pb.h和addressbook.pb.cc code
4, 成員訪問代碼blog
部分.h代碼及註釋:get
// name inline bool has_name() const; //字段是否存在,主要用於協議兼容等 inline void clear_name(); inline const ::std::string& name() const; inline void set_name(const ::std::string& value); inline void set_name(const char* value); inline ::std::string* mutable_name(); //如爲空,則new a buffer並返回之。 // id inline bool has_id() const; inline void clear_id(); inline int32_t id() const; inline void set_id(int32_t value); // email inline bool has_email() const; inline void clear_email(); inline const ::std::string& email() const; inline void set_email(const ::std::string& value); inline void set_email(const char* value); inline ::std::string* mutable_email(); // phone inline int phone_size() const; //repeated 字段的數量 inline void clear_phone(); inline const ::google::protobuf::RepeatedPtrField< ::tutorial::Person_PhoneNumber >& phone() const; inline ::google::protobuf::RepeatedPtrField< ::tutorial::Person_PhoneNumber >* mutable_phone(); inline const ::tutorial::Person_PhoneNumber& phone(int index) const; //訪問第index個字段 inline ::tutorial::Person_PhoneNumber* mutable_phone(int index); inline ::tutorial::Person_PhoneNumber* add_phone();
5, 用法
string
就不說了吧,見上方.h代碼段及註釋,比什麼例子要清楚多了。it