title: Alternative to virtual variables
body: <p>I have two classes, <code>Base</code> and <code>Derived</code>. <code>Derived</code> inherits from <code>Base</code> and additionally includes several functions and variables -- therefore the need to have two separate classes. However, they do share one function, <code>run</code>.</p>
  
  <p>In the example below I have to pass an argument to <code>run</code> in order to execute <code>read</code> - this argument depends on which class the object refers to. Is it possible to write a generic version of <code>read</code> such that the program automatically uses <code>vars_Base</code> or <code>vars_Derived</code> depending on which object calls <code>run</code>?</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;vector&gt;
  
  class Base
  {
    protected:
      void read(std::vector&lt;std::string&gt;);
  
    public:
      void run(std::vector&lt;std::string&gt; vars) { read(vars); }
      std::vector&lt;std::string&gt; vars_Base;
  };
  
  void Base::read(std::vector&lt;std::string&gt; int_vars)
  {
      for (auto int_vars_it : int_vars)
      {
          std::cout &lt;&lt; int_vars_it &lt;&lt; "\n";
      }
  }
  
  class Derived : public Base
  {
    protected:
    public:
      std::vector&lt;std::string&gt; vars_Derived;
      ///Here are other functions only known to Derived, not Base
  };
  
  int main()
  {
      Base b;
      b.vars_Base.push_back("aB");
      b.vars_Base.push_back("bB");
      b.vars_Base.push_back("cB");
      b.run(b.vars_Base);
  
      Derived d;
      d.vars_Derived.push_back("aD");
      d.vars_Derived.push_back("bD");
      d.vars_Derived.push_back("cD");
      d.run(d.vars_Derived);
  
      return 0;
  }
  </code></pre>