I dont get error when I access a private member variable of an object that was created in static function of that class. Consider below code.
#include <stdio.h>
class fruit{
private: int a;
public:
fruit(){
a = 2;
b = 3;
}
static void set(){
fruit f;
printf("%d",f.a); // no error; why? (a is still in private)
}
};
void call(){
fruit f;
printf("%d",f.a); // error: a is private; works as I expect
}
All I can understand is when a new object is created in static function of that corresponding class, you can even access private variables as public mode? but doesn't this break access specifiers concept? Or is there something Iam understanding wrong. No book pointed me this statement.
All these days I was under assumption that to access private data, you need object and non static method but after compiling the above code I was wrong. It seems like you dont need non static method. I tried even compiling the same on codepad.org http://codepad.org/H06FyRLL gave me o/p as 2. Kindly help me is this valid? and could anyone suggest some good tutorials to learn such hidden secrets?