1

Is there a simple way to initialize a proto message with empty fields in C++?

Example:

Message:

message CardInfo {
    string name = 1;
    uint32 amount = 2;
}

Print:

CardInfo {
    name: "",
    amount: 0
}

1 Answer 1

0

Protobufs are initialized with default values. Strings are empty and ints are 0.

Note, however, that default values might not be printed.

Try this:

CardInfo info;
std::cout << info.DebugString();
2
  • Ok, I create message, and I should use CopyFrom to set an empty field in my current message?
    – Leonid
    Commented Aug 3 at 7:07
  • Your question does not mention any "current message" or copying. This sounds like a separate question which will need some more context to answer. Commented Aug 3 at 7:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.