Inheriting from STL containers

The power of instantly creating a sequence of elements is amazing, and it makes you realize how much time you may have spent (or rather wasted) in the past solving this particular problem. For example, many utility programs involve reading a file into memory, modifying the file, and writing it back out to disk. You might as well take the functionality in StringVector.cpp and package it into a class for later reuse.

Now the question is: do you create a member object of type vector, or do you inherit? A general guideline is to always prefer composition (member objects) over inheritance, but with the STL this is often not true, because so many existing algorithms work with the STL types that you may want your new type to be an STL type. So the list of strings should also be a vector, thus inheritance is desired.

Note the careful avoidance of a global using namespace std statement here, to prevent the opening of the std namespace to every file that includes this header.
The constructor opens the file and reads it into the FileEditor, and write( ) puts the vector of string onto any ostream. Notice in write( ) that you can have a default argument for the reference.
The implementation is quite simple:

The functions from StringVector.cpp are simply repackaged. Often this is the way classes evolve?you start by creating a program to solve a particular application and then discover some commonly used functionality within the program that can be turned into a class.
The line-numbering program can now be rewritten using FileEditor:

(or in the open( ) member function), and writing happens in the single line (which defaults to sending the output to cout):


The bulk of the program is involved with actually modifying the file in memory.