title: C++ Using two variables of same name in Derived and Base classes
body: <p>I've posted about this problem before (<a href="https://stackoverflow.com/questions/17539155/c-polymorphic-inherited-attribute-class-variables-across-base-and-derived-clas?noredirect=1#comment25509712_17539155">Here</a>), this is a different approach to a solution. This solution to be seems to better encapsulate the behavior for those implementing the class, since it prevents them from needing to explicitly up cast.</p>
  
  <p>Here's the problem:</p>
  
  <p>I have a project in which I want to isolate core behavior in most objects, while providing additional behavior through derived objects. Simple enough:</p>
  
  <pre><code>class BaseA
  {
      virtual void something() {}
  }
  
  
  class DerivedA : public BaseA
  {
      void something() {}
      void somethingElse() {}
  }
  </code></pre>
  
  <p>Now let’s say I also have a second set of classes, same inheritance scheme except that they aggregate the above classes. However, I would like the base version to use the base class, and the derived version in the derived class. My solution i was thinking about 'hiding' the base class variable by using the same name;</p>
  
  <pre><code>class BaseB
  {
      BaseA *var;
  
      BaseB()
      {
          var = new BaseA();
      }
  
      virtual void anotherThing1();
      virtual void anotherThing2();
      virtual void anotherThing3();
  }
  
  class DerivedB : public BaseB
  {
      DerivedA *var;
  
      DerivedB()
      {
          var = new DerivedA();
      }
  
      void anotherThing1();
      void anotherThing2();
      void anotherThing3();
      void andAnother1();
      void andAnother2();
  }
  </code></pre>
  
  <p>The goal for this approach is so that functions that rely on the derived aggregated class no longer need to explicitly cast to achieve the gained functionality.</p>
  
  <pre><code>void func1( BaseB &amp;b )
  {
      b.anotherThing1();
      b.var-&gt;something();
  }
  
  void func2( DerivedB &amp;b )
  {
      b.anotherThing1();
      b.andAnother1();
      b.var-&gt;something();
      b.var-&gt;somethingElse();
  }
  
  void main( int argc, char **argv )
  {
      BaseB    baseB;
      DerivedB derivedB;
  
      func1( baseB );
      func1( derivedB );
      func2( derivedB );
  }
  </code></pre>
  
  <p>Would this be considered bad practice? </p>