How to assign a singel nested object value in C++?

24 views
Skip to first unread message

Nigel Pickard

unread,
Dec 29, 2009, 2:11:06 PM12/29/09
to Protocol Buffers
I'm having a problem working out how to assign a nested object's value
in C++ (my Java version works fine). E.g. in the proto file for my
car object I define:

import "mycustomengine.proto";
message MyCar {
optional string name = 1;
optional string model = 2;
optional MyCustomEngine customengine = 3;

Note I have assigned there to only be 0 or 1 MyCustomEngines to a Car
object. Now in my code, I've already created an instance of
MyCustomEngine, e.g.:

my::objects::MyCustomEngine myCustomEngine;
myCustomEngine.set_name("hey, this is my custom engine!");

Now I create my Car object....

my::objects::MyCar myCar;
myCar.set_name("Ford");
myCar.set_model("Fiesta");

But now I'm stuck on how to assign the myCustomEngine instance to the
myCar instance? There appears to be no method such as
"myCar.set_customengine(myCustomEngine)". So any ideas on how to do
this?

Henner Zeller

unread,
Dec 29, 2009, 2:32:24 PM12/29/09
to Nigel Pickard, Protocol Buffers

it is something along the lines of
myCar.mutable_customengine()->CopyFrom(myCustomEngine).

Since this copies data, you might want to consider setting things
directly in the customengine in the first place - if this is time
critical.

void InitCustomEngine(MyCustomEngine *engine) {
engine->set_name("My cool custom engine");
}

...
myCar.set_name("foo");
InitCustomEngine(myCar.mutable_customengine());

> this?
>
> --
>
> You received this message because you are subscribed to the Google Groups "Protocol Buffers" group.
> To post to this group, send email to prot...@googlegroups.com.
> To unsubscribe from this group, send email to protobuf+u...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/protobuf?hl=en.
>
>
>

Nigel Pickard

unread,
Dec 29, 2009, 2:48:57 PM12/29/09
to Protocol Buffers
Henner:

excellent, that worked.... I knew someone must have done that
already!

Thanks again,

Nigel

Reply all
Reply to author
Forward
0 new messages