auto src = ProtoSrc(); auto dst = ProtoDst(); auto srcFeature = src.feature(); auto &dstFeature = *dst.mutable_feature(); // 👇错误操作,会有类似报错: Tried to merge messages of different types (merge protosrc.Feature to protodst.Feature) // dstFeature.CopyFrom(srcFeature); // 👇正确操作 dstFeature.mutable_data()->CopyFrom(srcFeature.data());
复杂类型复制
以下文件模拟了两个不同proto文件中同时定义了相同的结构体Result
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
message ProtoSrc { message Result { optionalfloat data = 1; } repeated Result results = 1; } message ProtoDst { message Result { optionalfloat data = 1; } repeated Result results = 1; }
1 2 3 4 5 6 7
auto src = ProtoSrc(); auto dst = ProtoDst(); auto &dstRes = *(dst.mutable_results()); for (auto &srcRes : *(src.mutable_results())){ auto &temp = *(dstRes.Add()); temp.set_data(srcRes.data()); }