Effective C ++ 2e Item 30

zhaozj2021-02-11  186

Terms 30: Avoid such a member function: The return value is a non-Const pointer or reference to the member, but the member's access level is lower than this function.

The reason why a member is private or protected is to limit access to it, right? Tired compilers offer the force of Nine Niu Erhu to ensure that the access restrictions you set is not destroyed, right? Therefore, write a function to let users access the restricted members, do not significant, right? If you really think make sense, please read this paragraph repeatedly until you don't think so.

It is easy to violate this rule in actual programming. Here is an example:

Class address {...}; // someone live here

Class Person {public: address & personaddress () {return address;} ...

PRIVATE: Address Address; ...};

Members Functions Personaddress provide the caller with the Address object included in the Person object, but may be considered in efficiency, and the return result is referenced, not a value (see clause 22). Unfortunately, this member function is in the original intention of the Person :: Address declared by Private ::

Person Scott (...); // To simplify the parameters

Address & Addr = // Assume AddR to global variables Scott.PersonAddress ();

Now, the global object AddR has become another name of Scott.Address, and it can read and write Scott.Address. In fact, Scott.Address is no longer private, but the root source of the access level is the root of the member function Personaddress. Of course, the Access level of the Address given in this example is Private, if it is protected, the situation is exactly.

Not only is referenced, the pointer will also generate the above problems. The following example is the same as above, but this time is the pointer:

Class Person {public: address * personaddress () {return & address;} ...

PRIVATE: Address Address; ...};

Address * addrptr = Scott.PersonAddress (); // The problem is the same as above

Moreover, for pointers, we must worry not just data members, but also considering member functions. It is also possible because of the pointer to returns a member function:

Class Person; // Notice in advance

// ppmf = "Pointer to Person Member Function" // (pointing to the Person Member Function) TYPEDEF VOID (Person :: * PPMF) ();

Class Person {public: static ppmf verificationfunction () {return & person :: verifyaddress;}

...

PRIVATE: ADDRESS ADDRESS;

Void verifyaddress ();

}

If you haven't tried the usage of the member function pointer and TypeDef, you may think that Person :: verificationFunction is a bit scary. Don't be scared, it's all: · VerificationFunction is a member function without input parameters · It is a pointer to a member function in the Person class (ie, the return value of VerificationFunction) is not entered. The parameter is not returned, that is, Void.

As for the Static keyword, when it is used to declare a member, its meaning is: the entire class only has a copy of this member, and this member can be accessed by the specific object of the class. For a complete introduction of static, you can refer to the C tutorial. (If you don't introduce static members in your C tutorial, please tear the book page and throw it to the garbage collection station. Note that the cover must not throw it to avoid the environment. Finally, borrow or buy a better tutorial.)

In the last example, VerifyAddress is a private member function, which means it is just an implementation details of the class, only the members of the class should know it (of course, friends also know). However, since the public member function verificationFunction returns a pointer to VerifyAddress, users can do this:

PPMF PMF = Scott.VerificationFunction ();

(Scott. * PMF) (); // is equivalent to call // scott.verifyaddress

Here, PMF has become a synonym of Person :: VerifyAddress, just an important difference: it can be used without restrictions.

Although there is so much before, one day you may have a function of the program, the performance of the program -------- return value is a pointer or reference to a member of a member of a certain visit. But at the same time, you don't want to sacrifice Private and protected to provide access restrictions. In this case, you can reach two full beauty effects by returning a pointer or reference to the Const object. See Terms 21 for details.

转载请注明原文地址:https://www.9cbs.com/read-4446.html

New Post(0)