title: Accessing Private non-static class variable from Static member function - C++
body: <p>Are private non static variable/methods accessible in static function? If yes,
  then what is the use of "Private" access modifier?. Please go through the below code.</p>
  
  <pre class="lang-cpp prettyprint-override"><code>// Testclassheader.h file
  class TestClass{
  private:
  int TestVariable;    //Private Variable
  int TestFunction();   //Private Method
  
  public:
  static void TeststaticFn();   //Static Method
  
  };
  
  void TestClass::TeststaticFn()
  {
     TestClass TestObj;
     TestObj.TestVariable = 10; // Compiles perfectly
     TestObj.TestFunction(); //Compiles Perfectly
  
  }
  
  // Another Class
  //#include "Testclassheader.h"
  class AnotherClass{
  public:
  int DummyFunc();
  };
  
  int AnotherClass::DummyFunc()
  {
    TestClass AnotherObj;
    AnotherObj.TestVariable = 15; //error: 'TestVariable' is a private member of 'TestClass'
    AnotherObj.TestFunction();    //error: 'TestFunction' is a private member of 'TestClass'
  }
  
  </code></pre>
  
  <p>I tried the above code in Visual studio 12. Can anyone explain why private variable/methods are accessible in static method( which actually it should not)?</p>