Filling a container with objects using pointers / iterators
std::list is usually implemented as a doubly-linked list which is very inefficient for your purpose.  You should definitely use std::vector for this (or similar dynamic array structure, possibly some stream).

Your second approach is the idea I got while reading your question. You don't need an iterator (and I don't think you can call push_back on it), but a reference to the vector - vector&lt;byte&gt;&amp; buffer as the argument.

The same applies to serizalize() on the main list with possible default argument creating the vector. If you want to have the final packet somewhere, you can track some dirty flag (if you can modify the structure) to re-serialize it as needed.



You could do that last approach in two phases:


Track the sizes to compute the total size needed
Allocate the buffer (you could use byte* or vector)
Do the same again, but passing index together with the vector (buf[i++] = data)


...it is a bit more low-level solution, but possibly fastest. I would personally not worry that much and use the direct approach with vector (it should be fast enought).



Some notes about errors in the code:

class Sublist {
  std::vector&lt;Items*&gt; items;
  std::list&lt;unsigned char&gt; subpacket;

  std::list&lt;unsigned char&gt; Sublist::serialize() {


You do use Sublist:: prefix when you define the methoud outside of the class, not here (remove it).

class Sublist { ...
    std::vector&lt;unsigned char&gt;&amp; serialize(std::vector&lt;unsigned char&gt;&amp; buffer) const { ...


This should be the final signature (taking reference, returning the reference and with const modifier on the method because you are not modifying the class).