Avoid such a member function: the returned value is a non-const pointer or reference to the member, but the access level of the member is lower than that of the function.

Source: Internet
Author: User

The reason for making a member private or protected is to restrict access to it, right? The exhausting compiler requires a lot of effort to ensure that the access restrictions you set are not broken, right? Therefore, it doesn't make much sense to write a function to allow users to access restricted members at will, right? If you do think it makes sense, read this section repeatedly until you do not think so.

In actual programming, this rule is easily violated. The following is an example:

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

Class person {
Public:
Address & personaddress () {return address ;}
...

PRIVATE:
Address;
...
};

The member function personaddress provides the caller with the address object contained in the person object. However, for efficiency reasons, the returned results are referenced, rather than the value (see clause 22 ). Unfortunately, this member function violates the original intention of declaring person: address as private:

Person Scott (...); // The parameter is omitted to simplify

Address & ADDR = // assume that ADDR is a global variable.
Scott. personaddress ();

Now, the Global Object ADDR is another name of Scott. Address, which can be used to read and write Scott. Address at will. In fact, Scott. Address is no longer private, but public. The root cause of access-level improvement is 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 the same.

Not only references, but pointers also produce the above problems. The example below is the same as above, but this time we use a pointer:

Class person {
Public:
Address * personaddress () {return & address ;}
...

PRIVATE:
Address;
...
};

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

In addition, for pointers, you should not only worry about data members, but also consider member functions. It is also possible to return a pointer to a member function:

Class person; // declare in advance

// Ppmf = "pointer to person member function"
// (Pointer to the person member function)
Typedef void (person: * ppmf )();

Class person {
Public:
Static ppmf verificationfunction ()
{Return & person: verifyaddress ;}

...

PRIVATE:
Address;

Void verifyaddress ();

};

If you haven't tried to combine the member function pointer and typedef as above, you may think that the declaration of person: verificationfunction is a little scary. Don't be afraid. Its full meaning is only:

· Verificationfunction is a member function without input parameters.
· Its return value is a pointer to a member function in the person class.
· The function to be pointed to (that is, the return value of verificationfunction) has no input parameter and no return value, that is, void.

As for the static keyword, when it is used to declare a member, its meaning is: the entire class only has one copy of this Member, and this member can be accessed without a specific object of the class. For a complete introduction to static, refer to the C ++ tutorial. (If you haven't introduced static members in your c ++ tutorial, Please Tear down the page and throw it to the recycle bin. Do not litter the cover to avoid damage to the environment. Finally, borrow or buy a better tutorial .)

In the last example, verifyaddress is a private member function, which means that it is only an implementation detail of the class, and only the members of the class should know it (of course, friends also know ). However, because the public member function verificationfunction returns a pointer to verifyaddress, you can do this:

Ppmf PMF = Scott. verificationfunction ();

(Scott. * PMF) (); // equivalent to calling
// Scott. verifyaddress

Here, PMF becomes a synonym for person: verifyaddress, but there is an important difference: it can be used without restrictions.

Although I have mentioned so much, one day you mayProgramThe performance still has to write the function -------- the returned value is a pointer or reference of a lower access level member. At the same time, you do not want to sacrifice the access restrictions that private and protected provide for you. In this case, you can achieve the best of both worlds by returning a pointer or reference to the const object.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.