title: Access derived class member variable in virtual function
body: <pre><code>class Car {
      class BaseState {
         explicit BaseState(Car* vehicle) : mVehicle(vehicle) {}
         virtual void run() = 0;
  
         Car* mVehicle;
      }
      class State1 : public BaseState {
         explicit State1(Car* vehicle) : BaseState(vehicle) {}
         virtual void run() {
             // use data of Car
             ...
             doSomething();
         }
         virtual void doSomething() {
         }
      }
      class State2 : public BaseState {
      }
      ...
  }
  
  class Convertible: public Car {
      class State1 : public Car::State1 {
         explicit State1(Convertible* vehicle) : Car::State1(vehicle) {}
         virtual void doSomething() {
             static_cast&lt;Convertible*&gt;(mVehicle)-&gt;foldTop();
         }
      }
      class State2 : public Car::State2 {
      }
      ...
      void foldTop() {}
  }
  </code></pre>
  
  <p>All States are derived from the BaseState so that they have the member variable mVehicle to access outer class variables.
  However, in each derived class, in all functions of each State, static_cast is needed to access derived class member variables and functions.</p>
  
  <p>Any better solution?</p>
  
  <ol>
  <li>In each State of derived classes, add another pointer (e.g., Convertible *mConvertible). Each State has duplicate pointers (mConvertible and mVehicle) point to the same object. Does not look right.</li>
  <li>Use a virtual Getter instead of mVehicle in base class. There will be excessive Getter calls in base class.</li>
  </ol>
  
  <p>=======================================================================</p>
  
  <p>Yes. I tried template as below, but it cannot be compiled because errors like</p>
  
  <p>"car.h: In member function ‘virtual void Car::State1::run()’:
  car.h:18:12: error: ‘mVehicle’ was not declared in this scope
  ".</p>
  
  <pre><code>// car.h
  #include &lt;iostream&gt;
  
  template &lt;class T&gt;
  class Car {
  public:
      class BaseState {
      public:
         explicit BaseState(T* vehicle) : mVehicle(vehicle) {}
  
      protected:
         T* mVehicle;
      };
  
      class State1 : public BaseState {
      public:
         explicit State1(T* vehicle) : BaseState(vehicle) {}
         virtual void run() {
             mVehicle-&gt;x = 1;
             mVehicle-&gt;y = 2;
             mVehicle-&gt;doSomething1();
             mVehicle-&gt;doSomething2();
             processEvent();
         }
         virtual void processEvent() {
             if (mVehicle-&gt;val &gt; 2) {
                  std::cout &lt;&lt; "too large" &lt;&lt; std::endl;
             }
         }
      };
  
      class State2 : public BaseState {
      public:
         explicit State2(T* vehicle) : BaseState(vehicle) {}
         virtual void run() {
             mVehicle-&gt;x = 10;
             mVehicle-&gt;y = 20;
             processEvent();
         }
         virtual void processEvent() {
             if (mVehicle-&gt;val &gt; 20) {
                  std::cout &lt;&lt; "too large" &lt;&lt; std::endl;
             }
         }
      };
  
      virtual void doSomething1() {
          val += x * y;
      }
  
      virtual void doSomething2() {
          val += x + y;
      }
  
  protected:
      int x;
      int y;
      int val;
  
  };
  
  // convertible.h
  #include "car.h"
  #include &lt;iostream&gt;
  
  class Convertible : public Car&lt;Convertible&gt; {
  protected:
      class State1 : public Car&lt;Convertible&gt;::State1 {
         explicit State1(Convertible* vehicle) : Car&lt;Convertible&gt;::State1(vehicle) {}
         // want to override functions in base class states
         virtual void processEvent() {
             if (mVehicle-&gt;val &gt; 10) {
                  std::cout &lt;&lt; "too large" &lt;&lt; std::endl;
                  mVehicle-&gt;val = 10;
             }
         }
      };
  
      // want to override some base class functions
      // and access some special variables
      // want to inherit other functions
      virtual void doSomething2() {
          z = 10;
          val += x + y + z;
      }
  
  protected:
      int z;
  };
  </code></pre>
  
  <p>If I use <code>State1(Car* vehicle)</code> instead of <code>State1(T* vehicle)</code>, there is additional conversion error. What am I doing wrong?</p>
  
  <p>If the program can figure out that <code>Convertible::State1::processEvent()</code> should be executed, why cannot it automatically cast <code>mVehicle</code> from <code>Car*</code> to <code>Convertible*</code>? Apparently <code>mVehicle</code> points to a <code>Convertible</code> object when <code>Convertible::State1::processEvent()</code> is deduced. We do not need template if there is automatic cast.</p>