title: Optional Variable(variable structure) in structure in c++
body: <p>I have to implement a data structure in c++, which have optional variable.
  As  </p>
  
  <pre><code> struct xyz
   {
        int x;  //required
        int y;  //optional
        bool a;  // required
        bool b;  // optional
        bool c;  // optional
        std::string d; //optional
        std::string e; // required
        ........
  
   }
  </code></pre>
  
  <p>Some of the variables are required means fixed, but some of the variable is optional.</p>
  
  <p>I can't set any default value to the variables to tell its optional, e.g. bool variable has only two states and each state has a meaning for our project. And same for integer as well every value of integer is useful data for me.</p>
  
  <p>I googled but not found any satisfactory answer.</p>
  
  <p>I tried with <code>std::vector</code> but it not looks good.  </p>
  
  <pre><code> struct xyz
   {
        int x;  //required
        std::vector&lt;int&gt; y;  //optional
        bool a;  // required
        std::vector&lt;bool&gt; b;  // optional
        ........
   }
  </code></pre>
  
  <p>In this method we can check the size of vector, if zero means variable is not present else the variable has the desired value.</p>
  
  <p>But for a bit <code>bool</code> or 4 byte <code>int</code> creating a <code>std::vector</code> is overhead to data structure.</p>
  
  <p>Can any one suggest out of these methods? </p>