![](/img/trans.png)
[英]How can I assign the nested Protobuf messages' values to the main message? C++
[英]How to assign nested protobuf without copying in C++?
我有一個 protobuf,其中一個字段是另一個 protobuf,我已經准備好后一種類型的實例。 如何在不復制的情況下將此新實例注入另一個 protobuf?
我認為您正在從這里尋找 set_allocated_*
void set_allocated_foo(Bar* bar):將 Bar object 設置為字段並釋放前一個字段值(如果存在)。 如果 Bar 指針不是 NULL,則消息獲取分配的 Bar object 的所有權,並且 has_foo() 將返回 true。 否則,如果 Bar 為 NULL,則行為與調用 clear_foo() 相同。
如果您使用的是 proto3,則編譯后的 class 定義了移動構造函數。 所以你可以簡單地移動它。
原型定義:
syntax="proto3";
message A {
string s = 1;
}
message B {
A a = 1;
}
將 A 移入 B:
A a;
a.set_s("hello");
B b;
(*b.mutable_a()) = std::move(a);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.