title: C++ virtual variable in inheritance class hierarchy
body: <p>I have a  template class hierarchy,</p>
  
  <pre><code>                   ___ Class (ClassA)
                    |
  AbstractClass_____
  
                    |___ Class (ClassB)
  </code></pre>
  
  <p>in classA and ClassB, I have a const NullPosition of a templated type, which is different in ClassA and ClassB. In class classA and ClassB I have to do some operation which are dependant on the value of the NullPosition. </p>
  
  <p>Now I need to do some operations depending on the value on NullPosition, but I am having hard time since the variable are different type and values. To be more specific NullPosition in classA identifies an invalid array index, therefore equals -1;
  in classB it identifies a NULL pointer therefore it equals 0.</p>
  
  <p>Please find below an example.</p>
  
  <pre><code>#ifndef ABSTRACTCLASS_H
  #define  ABSTRACTCLASS_H
  
  template &lt;class T, class P&gt;
  class AbstractClass
  {
        public: 
  
        typedef T Type;
        typedef P Position;
  
        void MethodX() const;
        virtual Position Method() const = 0; 
  
  
  };
  
  template &lt;class T, class P&gt;
  void AbstractClass&lt;T,P&gt;::MethodX() const
  {
       Position p=Method();
  
       /*
       what I am trying to achieve is being able to use the constant NullPosition in abstract class.
  
       if (p==NullPosition)
       cout&lt;&lt;"p is equal NULLPOSITION";
       else
       cout&lt;&lt;"p is not equal NULLPOSITION";  
       */
  }
  
  #endif
  
  
  
  #ifndef CLASS_A_H
  #define  CLASS_A_H
  #include "../AbstractClass.h"
  
  template &lt;class T&gt;
  class Class:public AbstractClass&lt;T,unsigned int&gt;
  {
        public:
  
        typedef T Type;
        typedef typename AbstractClass&lt;T,unsigned int&gt;::Position Position;
  
        Class();
  
        Position Method() const;  
  
        static const Position NullPosition=-1;
  
        private:
                Type* TypeArray;
                unsigned int nElements;
  
  };
  
        template &lt;class T&gt;
        Class&lt;T&gt;::Class()
        {
         nElements=0;
         TypeArray=new Type[128];
        }
  
        template &lt;class T&gt;
        typename Class&lt;T&gt;::Position Class&lt;T&gt;::Method() const 
        {
  
         return NullPosition;
  
        }
  
  #endif
  
  
  
  
  #ifndef CLASS_B_H
  #define CLASS_B_H
  
  #include "../AbstractClass.h"
  
  template &lt;class T&gt;
  struct elementNode
  {
      typedef T Type;
      typedef elementNode* Position;
  
      Type element;
      Position nextNode;
  };
  
  template &lt;class T&gt;
  class Class:public AbstractClass&lt;T, typename elementNode&lt;T&gt;::Position&gt;
  {
        public:
  
        typedef T Type;
        typedef typename AbstractClass&lt;T, typename elementNode&lt;T&gt;::Position&gt;::Position Position;
  
        Class();
        Position Method() const; 
  
        static const Position NullPosition;
  
        private:
                Position root;
                Position lastElement;
  
  };
  
        template &lt;class T&gt;
        const typename Class&lt;T&gt;::Position Class&lt;T&gt;::NullPosition=0;
  
        template &lt;class T&gt;
        Class&lt;T&gt;::Class()
        {
         lastElement=root=NullPosition; 
        }
  
        template &lt;class T&gt;
        typename Class&lt;T&gt;::Position Class&lt;T&gt;::Method() const 
        {
  
         return NullPosition;
  
        }
  
  #endif
  
  
  #include &lt;cstdlib&gt;
  #include &lt;iostream&gt;
  
  using namespace std;
  
  
  #include "Class/ClassA/Class.h" 
  
  int main(int argc, char *argv[])
  {
      Class&lt;int&gt; classA;
  
      classA.MethodX();
  
      system("PAUSE");
      return EXIT_SUCCESS;
  }
  </code></pre>
  
  <p>Please not that ClassA and ClassB share the same name Class, so that I a can use them interchangeble in my code by changing just the include path - #include "Class/ClassA/Class.h" for classA and #include "Class/ClassB/Class.h" for classB.</p>