title: C++ protobuf how to set values for nested struct
body: <p>I am learning C++ ProtoBuf.</p>
  <p>I have the following struct which I need to serialize:</p>
  <pre><code>enum phonetype
  {
      DESKPHONE,
      MOBILE,
      WIRELESSPHONE
  };
  
  struct phonenumber
  {
      int ptype;
      string number;
  };
  
  struct address
  {
      string addr1;
      string addr2;
  };
  
  struct college
  {
      string collegename;
      string collegeaddress;
  };
  
  struct student
  {
      int id;
      string name;
      double age;
      string email;
  
      struct phonenumber phoneN;
      struct address addr;
      struct college col;
  };
  </code></pre>
  <p>I have initialized the struct as follows:</p>
  <pre><code>student stud = {123, &quot;Stud_1&quot;, 30, &quot;none&quot;,
                      {MOBILE, &quot;123456789&quot;}, 
                      {&quot;Boston, US&quot;, &quot;None&quot;}, 
                      {&quot;Boston college&quot;, &quot;Boston&quot;}};
  </code></pre>
  <p>Now I would like to create a serialized string of this struct for which I have written the following <code>.proto</code> file:</p>
  <pre><code>syntax = &quot;proto2&quot;;
  
  message studentP
  {
    required int32 id = 1;
    required string name = 2;
    required double age = 3;
    optional string email = 4;
  
    message phonenumberP
    {
      required int32 ptype = 1;
      required string number = 2;
    }
    
    message addressP {
      required string addr1 = 1;
      optional string addr2 = 2;
    }
  
    message collegeP {
      required string collegename = 1;
      optional string collegeaddress = 2;
    }  
  }
  </code></pre>
  <p>In my C++ code, I am setting the proto obj values as follows:</p>
  <pre><code>studentP studObj;
  studObj.set_name(stud.name);
  studObj.set_eid(stud.id);
  studObj.set_age(stud.age);
  
  studentP::phonenumberP *phone;
  phone-&gt;set_ptype(stud.phoneN.ptype);
  phone-&gt;set_number(stud.phoneN.number);
  
  studentP::addressP *addr;
  addr-&gt;set_addr1(stud.addr.addr1);
  addr-&gt;set_addr2(stud.addr.addr2);
  
  studentP::collegeP *coll;
  coll-&gt;set_collegename(stud.col.collegename);
  coll-&gt;set_collegeaddress(stud.col.collegeaddress);
  
  
  string student_str;
  studObj.SerializeToString(&amp;student_str);
  </code></pre>
  <p>Above I have separately set the values for the internal structs of class <code>studentP</code>.</p>
  <p><strong>How do I set the values for the internal structure of <code>studentP</code> object <code>studObj</code>?</strong></p>
  <p>Do I need to call <code>SerializeToString</code> for each internal struct?</p>