Member-Acess Control

来源:互联网 发布:淘宝2016下载 编辑:程序博客网 时间:2024/04/29 12:39

1. Controlling Access to Class Members

摘自MSDN:http://msdn.microsoft.com/en-us/library/zsc61976.aspx

You can increase the integrity of software built with C++ by helping control access to class member data and functions. Class members can be declared as having private, protected, or public access, as shown in the following table:

Member-Access Control

Type of Access

Meaning

private

Class members declared as private can be used only by member functions and friends (classes or functions) of the class.

protected

Class members declared as protected can be used by member functions and friends (classes or functions) of the class. Additionally, they can be used by classes derived from the class.

public

Class members declared as public can be used by any function.

Access control helps prevent you from using objects in ways they were not intended to be used. This protection is lost when explicit type conversions (casts) are performed.

NoteNote

Access control is equally applicable to all names: member functions, member data, nested classes, and enumerators.

The default access to class members (members of a class type declared using theclass keyword) is private; the default access tostruct andunion members is public. For either case, the current access level can be changed using thepublic,private, or protected keyword.

2. Access Specifiers for Base Classes

摘自MSDN:http://msdn.microsoft.com/en-us/library/7f45fat0.aspx

Two factors control which members of a base class are accessible in a derived class; these same factors control access to the inherited members in the derived class:

  • Whether the derived class declares the base class using the public access specifier in theclass-head (class-head is described in the Grammar section inDefining Class Types).

  • What the access to the member is in the base class.

The following table shows the interaction between these factors and how to determine base-class member access.

Member Access in Base Class

private

protected

Public

Always inaccessible regardless of derivation access

Private in derived class if you use private derivation

Private in derived class if you use private derivation

 

Protected in derived class if you use protected derivation

Protected in derived class if you use protected derivation

 

Protected in derived class if you use public derivation

Public in derived class if you use public derivation

The following example illustrates this:

// access_specifiers_for_base_classes.cppclass BaseClass{public:    int PublicFunc();    // Declare a public member.protected:    int ProtectedFunc(); // Declare a protected member.private:    int PrivateFunc();   // Declare a private member.};// Declare two classes derived from BaseClass.class DerivedClass1 : public BaseClass{};class DerivedClass2 : private BaseClass{};int main(){}

In DerivedClass1, the member function PublicFunc is a public member and ProtectedFunc is a protected member becauseBaseClass is a public base class.PrivateFunc is private toBaseClass, and it is inaccessible to any derived classes.

In DerivedClass2, the functions PublicFunc andProtectedFunc are considered private members becauseBaseClass is a private base class. Again, PrivateFunc is private toBaseClass, and it is inaccessible to any derived classes.

You can declare a derived class without a base-class access specifier. In such a case, the derivation is considered private if the derived class declaration uses theclass keyword. The derivation is considered public if the derived class declaration uses thestruct keyword. For example, the following code:

class Derived : Base...

is equivalent to:

class Derived : private Base...

Similarly, the following code:

struct Derived : Base...

is equivalent to:

struct Derived : public Base...

Note that members declared as having private access are not accessible to functions or derived classes unless those functions or classes are declared using thefriend declaration in the base class.

A union type cannot have a base class.

NoteNote

When specifying a private base class, it is advisable to explicitly use the private keyword so users of the derived class understand the member access.

3. Access Control and Static Members

摘自MSDN:http://msdn.microsoft.com/en-us/library/5bw4yh4d.asp

When you specify a base class as private, it affects only nonstatic members. Public static members are still accessible in the derived classes. However, accessing members of the base class using pointers, references, or objects can require a conversion, at which time access control is again applied. Consider the following example:

// access_control.cppclass Base{public:    int Print();             // Nonstatic member.    static int CountOf();    // Static member.};// Derived1 declares Base as a private base class.class Derived1 : private Base{};// Derived2 declares Derived1 as a public base class.class Derived2 : public Derived1{    int ShowCount();    // Nonstatic member.};// Define ShowCount function for Derived2.int Derived2::ShowCount(){   // Call static member function CountOf explicitly.    int cCount = Base::CountOf();     // OK.   // Call static member function CountOf using pointer.    cCount = this->CountOf();  // C2247. Conversion of                               //  Derived2 * to Base * not                               //  permitted.    return cCount;}

In the preceding code, access control prohibits conversion from a pointer to Derived2 to a pointer to Base. The this pointer is implicitly of typeDerived2 *. To select theCountOf function,this must be converted to typeBase *. Such a conversion is not permitted becauseBase is a private indirect base class toDerived2. Conversion to a private base class type is acceptable only for pointers to immediate derived classes. Therefore, pointers of typeDerived1 * can be converted to typeBase *.

Note that calling the CountOf function explicitly, without using a pointer, reference, or object to select it, implies no conversion. Therefore, the call is allowed.

Members and friends of a derived class, T, can convert a pointer toT to a pointer to a private direct base class ofT.




0 0
原创粉丝点击