简体   繁体   中英

How to set a required protobuf field to its default value?

在protobuf 2 / C ++中,如何将必填消息字段设置为其默认值?

You can use the Clear method to reset the field to default value:

message Msg {
    required int32 i = 1 [default = 10];
};

Msg msg;
msg.set_i(123);

msg.Clear(); // Set the field to default value: 10.

// Another way:
msg.clear_i();

However, this will make the required field cleared, ie msg.has_i() == false . If you want to make it being set, also do the following:

msg.Clear();
msg.set_i(msg.i());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM