title: Dynamic allocation of vectors in a class
body: <p>If I have a class that contains a vector of another class and I'm expecting it to be very long:</p>
  
  <pre><code>class NucleotideSequence{
  private:
      std::string Name;
      std::vector&lt;Nucleotide&gt; Sequence;
  public:
      NucleotideSequence();
      NucleotideSequence(std::string name, std::vector&lt;Nucleotide&gt; seq);
      std::string getName();
      Nucleotide* getBase(int pos1);
      int getLength();
      void print();
  };
  </code></pre>
  
  <p>In this case the vector Sequence, do I need to dynamically allocate it by making Sequence *Sequence and making a new vector during the constructor? I want to make sure I use the right resource (stack vs heap) for large vectors (over hundreds of thousands of elements). Which is the right thing to do? I heard vectors wrap the dynamic array allocation.</p>
  
  <p>EDIT:</p>
  
  <p>I've provided the updated code below to show I've used reference passing for the constructor. I hope to use a move constructor too so I can make these objects in a function and then move them outside.</p>
  
  <p>Also given is the updated getPos method which throws the error if the position does not exist in the sequence.</p>
  
  <pre><code>class NucleotideSequence{
  private:
      std::string Name;
      std::vector&lt;Nucleotide&gt; Sequence;
  public:
      NucleotideSequence();
      NucleotideSequence(const std::string &amp;name, const std::vector&lt;Nucleotide&gt; &amp;seq); // Note that a pointer is not needed since the std::vector class allocated memory on the heap for us and is a wrapper for that whole RAII process.
      std::string getName();
      Nucleotide getBase(int pos);
      int getLength();
      void print();
  };
  
  NucleotideSequence::NucleotideSequence(const std::string &amp;name, const std::vector&lt;Nucleotide&gt; &amp;seq)
  {
      Name = name;
      Sequence = seq;
  }
  
  // Get a specific base
  Nucleotide NucleotideSequence::getBase(int pos)
  {
      for(std::vector&lt;Nucleotide&gt;::iterator i = Sequence.begin(); i != Sequence.end(); i++)
      {
          if(pos == i-&gt;getPos())
          {
              return *i; // Return the correct nucleotide object.
          }
      }
      throw BoundsError(); // If the desired position is not found, throw the error.
  }
  </code></pre>
  
  <p>Thanks,
  Ben.</p>