Protected in C ++

Source: Internet
Author: User

"... By the way, Mark Linton came to my office and made an impressive request to provide a third level of control, to support the styles used in the Interviews library being developed by Stanford University. Let's speculate together to create the word protected to represent some Members in the class ,..."

"... Mark is the main designer of Interviews. His persuasive argument is based on actual experience and examples from real code ...."

"... About five years later, Mark banned protected data members in Interviews because they have become the root cause of many program errors ..."

I don't like protected, but today, when I use C ++ occasionally, there is no more cleanliness. It is difficult to use C ++ to make a stable design. The key is not to use C ++ as a special core.

Today, I encountered a problem related to protected. I think it can be written. This is a basic problem and seems familiar to me. After all, I haven't touched it for many years, and I am a little unfamiliar with the C ++ syntax.

When I was a child, I thought this code was illegal.

Because I am worried that the private member variable a of f cannot be accessed in foo: foobar.

Later, I understood that the so-called private is for classes, not specific objects.

But I encountered another problem today.

This time, in foobar: bar, the member accessing this is allowed, but the member of f is forbidden.

Because foo: a is protected for foobar, the member function of foobar can access its own a, but it is forbidden for the foo pointer.

Think about the solution.

Bad taste. However, this is not the case.

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.