(a) Why not use the public member variable
(1) First, in terms of grammatical consistency , the only way the client can access the object is through the member function, and the customer does not have to consider whether to remember to use parentheses ().
(2) Second, using a function allows us to control the processing of member variables more precisely. Suppose we make the member variable public, then everyone can read and write it.
But suppose we get or set its value as a function. We are able to achieve "no access", "Read Only" and "Read and write". We can even achieve "write-in".
Class Accesslevels {public://member NoAccess No matter what the interview action, to achieve no access to ask! int Getreadonlay () const {return readOnly;} Set this data member to read Only! void Setreadwrite (int value) {readWrite = value;} This function sets the data member to write an access question. int Getreadwrite () const {return readWrite;} This function sets the data member to be read-only! void setwriteonly (int value) {writeOnly = value;} This function sets the data member to write an access question! Private: int noAccess; int readOnly; int readWrite; int writeOnly; };
It is necessary to divide the access control in such a subtle way. Because many member variables should be hidden. Each member variable requires a getter function and a setter function is rare after all.
(3) finally. There is also encapsulation .
Assume that the member variable is visited through a function. You can replace this member variable with a calculation later, and the customer will not know that the implementation of the class has changed.
Member variables are hidden behind the function interfaces, providing resilience to "all possible implementations." This makes it easy to notify other objects when a member variable is read or written, to verify class constraints and functions, and to run synchronization control in a multithreaded environment.
。
。 Wait a minute.
Encapsulation is important. Assume that the customer hides the member variable (that is) encapsulated. reserves the right to change the implementation later. Public means not encapsulation, and not encapsulation means that it cannot be changed.
Two
The protected member variable lacks encapsulation as a public member variable: The encapsulation of the member variable is inversely proportional to the number of code destroyed when the content of the member variable is changed, and a public member variable is set to False. We canceled it. All the customer code that uses it will be destroyed, which is an unknown mass.
So the public member function is completely non-encapsulated. If a protected member variable, we cancel it. All the derived classes that use it will be destroyed. Often also an unknown mass.
Please remember:
(1) Remember to declare the member variable as private.
This gives the customer access to data consistency. Access control can be finely divided, the promise constraints are guaranteed, and the class author is provided with full flexibility.
(2) Protected is not more encapsulated than public.
Effective C + +: clause 22: Declaring a member variable as private