Set all fields of nested message in protobuf c++

53 views
Skip to first unread message

Gaurav Bhosale

unread,
Oct 16, 2019, 2:07:41 PM10/16/19
to Protocol Buffers
I have proto file ( target for C++ )
message Test1
{
message Test2
{
required double x = 1;
}
required double y = 1;

}


I want initialize Test1 message without using mutable_****
Or
Without using set fields function in C++

Means, is it possible that,

Test1 test_message;
test_message = new Test1();

and all fields initialized to zero or default value?
Is there any single function, which will do initialize all fields of nested message in C++?

Adam Cozzette

unread,
Oct 16, 2019, 2:16:29 PM10/16/19
to Gaurav Bhosale, Protocol Buffers
With some work you can implement this functionality using protobuf reflection. However I would strongly recommend against using required fields at all, if you can avoid them. Changing an existing field from required to optional is dangerous, but it's a good idea to avoid required fields from the start when creating new message types.

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/protobuf/d1d576a0-7944-408d-ae10-a0007d14cddd%40googlegroups.com.

Gaurav Bhosale

unread,
Oct 16, 2019, 2:24:00 PM10/16/19
to Protocol Buffers
Yes. I an able to do that with reflection but I want to initialize all fields to zero without reflection.
Is that possible?
So idea is to access all fields if Test1 message.
Currently, when I do
Test1 test_message;
test_message = new Test1();

I am not able to access its fields x and so on.
I wanted to ask all fields after memory allocation.

Adam Cozzette

unread,
Oct 16, 2019, 2:36:58 PM10/16/19
to Gaurav Bhosale, Protocol Buffers
There is no way to do this without using reflection or the generated accessors (i.e. set_* and mutable_*).

--
You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
To unsubscribe from this group and stop receiving emails from it, send an email to protobuf+u...@googlegroups.com.

Gaurav Bhosale

unread,
Oct 16, 2019, 2:39:40 PM10/16/19
to Protocol Buffers
Okay. Thanks for your feedback.
Reply all
Reply to author
Forward
0 new messages