Who has access to class members?
There are three levels of access to members
private : access is granted only to the class' methods and to friend functions
and classes (Question 6.17 explains friend).
protected : access is granted only to the methods and to derived classes' meth-
ods.
public : access is granted to everyone.
Restricting access to members is usefull for detecting illicit use of the members of a
class when compiling, as shown in the following code.
class A
{
private:
int a0;
void f0() { /* ... */ }
protected:
int a1;
void f1() { f0(); } // ok
public:
int a2;
void f2() { /* ... */ }
};
void function()
{
A a;
a.a0 = 0; // error
a.f0(); // error
a.a1 = 0; // error
4
a.f1(); // error
a.a2 = 0; // ok
a.f2(); // ok
}
0 comments:
Post a Comment