C ++ object type Notes: private, protected, public

Source: Internet
Author: User

Some people often ask during interviews:C ++How can I fail?PublicFunction to obtainPrivate?

Of course, there are many answers, such as the following two methods:

(1),# Define private public;
# Define class struct
(You can add Macros in the compilation environment (including GCC and vs2005, or other compilers). The specific method is Google search. This note is not about specific operations)

(2), Write one and classAIdentical structAstruct, Delete all permission symbols:

  1. ClassA
  2. {
  3. Private:
  4. IntA;// What should I do if I want to access?
  5. };

  6. StructAstruct
  7. {
  8. IntAs;
  9. };

  10. A aa;
  11. // For information on pointer type conversion, refer to the Note: pointer type conversion"
  12. StructAstruct * pastruct = (StructAstruct *) & aa;

  13. Pastruct-> As = 8;// Actually it is aa. A = 8, that is, the private data int A in a is accessed.


These methods are acceptable,,

What is behind this?

What is the purpose of this technical question?

Have you ever thought about it?

Actually, thisPrivate,Public, AndProtected, Or evenConst,Static,InlineOnly works during compilation!In other wordsProgramClassAObjectAThere is no information about these permissions, and the layout of member variables is left!So,Astruct * P = (astruct *) &;Can be successfully accessed at runtimeAPrivate member.

As for the first method, it is also a truth, because these access operators do not occupy any details during the connection and runtime, so we assume that the classAThe target file is provided in the class library.AInAUsed in declared header files# Define
Class struct, # define private public
To accessAIn the same connection and operation phase, the technical staff asked this question for this purpose.--Understand yourC ++The principle of the compilation and running mechanism.

Why does the compiler not add runtime protection mechanisms? No. ActuallyC ++The standard does not specify how to implement it, but due to the role of the market economy, each compiler vendor is competing with each other.C ++The goal of the language is to be compatibleCTo achieveCBecause it should be compatible as much as possibleCSo you cannot add too many things to the running memory (think aboutVptrAlreadyC ++You can't add more protection operations because it is highly efficient (this is the same as that of arrays without cross-border access)

The conclusion is:Private,Public,ProtectedIt is the role played during the compilation process. After the compilation phase, the target file is generated, and the information disappears without a trace. Therefore, the above method takes effect.

Therefore, when a class library is used, if the access of Private Members is too troublesome (it is a little troublesome, and a lot of get/set is written throughout the day ~~), You can use # define to access it directly. However, if your boss sees any consequences caused by this, do not blame it ~. It is not responsible for the partition, haha.

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.