Non-friend/non-member functions cannot access private data of a class : private « Class « C++ Tutorial






#include <iostream>
using std::cout;
using std::endl;

class MyClass 
{
public:
   MyClass() : x( 0 )
   { 
   }
   void print() const       
   { 
      cout << x << endl; 
   }
private:
   int x;
}; 

void cannotSetX( MyClass &c, int val )
{
   //c.x = val; // ERROR: cannot access private member in MyClass
}

int main()
{
   MyClass counter;

   cannotSetX( counter, 3 );
   return 0;
}








9.9.private
9.9.1.private fields
9.9.2.private by default
9.9.3.Non-friend/non-member functions cannot access private data of a class
9.9.4.Using a class with private data members