title: How to best modify a group of member objects the same way?
body: <p>Maybe I'm being to picky, but I would like to have your opinion on how to best code something like this (my problem is much more complicated but it can be simplified like this): </p>
  
  <pre><code>class A
  {
  public:
          A ();
          A (const size_t dim0, const size_t dim1);
  
          void set_is_x_allocated (const bool);
          void set_is_y_allocated (const bool);
          void set_is_z_allocated (const bool);
  
  private:
          void allocate (vector&lt;vector&lt;double&gt;&gt; &amp;) const; ??
  
          size_t dim0;
          size_t dim1;
  
          bool is_x_allocated;
          bool is_y_allocated;
          bool is_z_allocated;
  
          vector&lt;vector&lt;double&gt;&gt; x;
          vector&lt;vector&lt;double&gt;&gt; y;
          vector&lt;vector&lt;double&gt;&gt; z;
  };
  
  </code></pre>
  
  <p>When the dimensions are given, I would like to allocate x, y, z the same way (depending on how the booleans is_x/y/z_allocated are set). The solutions I have are those at the moment: </p>
  
  <p>Solution 1: Create an allocate function like above, and call <code>if (is_x_allocated) allocate (x);</code> etc.</p>
  
  <p>Pros: x, y, z will be allocated the same way -> easier to spot errors, to modify the code if a third dimension comes in in the future. </p>
  
  <p>Cons: as allocate can be used for any other <code>vector&lt;vector&lt;double&gt;&gt;</code>, should it be <code>const</code> or not?</p>
  
  <p>Solution 2: Create an allocate_x (), allocate_y (), allocate_z (), call <code>if (is_x_allocated) allocate_x();</code></p>
  
  <p>Pros: It's adapted to the situation and allocate_x/y/z are definitely non-constant. </p>
  
  <p>Cons: I have to copy the allocation code 3 times. I might make some mistakes + the code will be more complicated to modify if a 3rd dimension comes in for example. </p>
  
  <p>How would you code this in an effective way? Maybe a lambda function inside a <code>void allocate ()</code> function, but I feel like this would be making a too big deal of what it really is... </p>