What are the advantages and disadvantages of static and private ???

Source: Internet
Author: User

Static functions can be referenced directly using classname: funname without being constructed. Private functions must be constructed before being referenced by internal functions of the class. Static functions should also be public.

Others. Wait for the lecture downstairs ......

Likewise, static member variables are also public and exist without building objects.

Static variables are like friends and friends of the same person. The function can keep the original values.
The private variable is like a passenger of a car. The next entry changes people. Every entry to the function is an initial value.

Static variables belong to the class, that is, all objects of this class share a memory zone.
For example, if you design a deposit class, each account is a class object, and each account has an account, you can use common variables. But for the deposit interest rate, everyone is the same, only one must be shared, and the static type is used. In this way, you only need to change the interest rate once. If it is defined as a non-static type, you have to do this for each account.

The private type is used for information hiding. Defining non-essential variables outside the class as the private type can prevent misoperation.

I forgot to add one sentence above. Static belongs to the class and non-static belongs to the object.

C ++ primer is very serious upstairs.

Thank you for your enthusiasm, but the younger brother believes that there is no essential difference in their role. Can you give me an example to understand the advantages and disadvantages of each other?

You can use the class name and method name to access static data without using new data.
A private instance can only be accessed by a new user, and can only be accessed by the member functions of the class. Neither the subclass nor the instance of the class can be accessed.

If Class B inherits from Class A, the static function and members of Class A will continue to be valid in Class B, but the private function and members of Class A cannot be referenced in Class B.

We recommend that you write your own code and try it out. Both reading and practice are complementary.

Oh, no difference ???
Private Members do not exist before object construction, that is, they can only be as follows:
Tyourobj OBJ;
OBJ. setprivmember (1 );
Or
Tyourobj * OBJ = new tyourobj;
Tyourojb-> setprivmember (1 );
Before defining an object, you cannot operate its private variable. Each object has a memory implementation of this variable,

For static members, all objects share a memory implementation, and the objects in this class are no longer created when they exist, just like global variables.
For example:
Tyourobj: staticmember = 1;
Unlike global variables, static members can also be private, so that information is hidden.

Static member functions are different from common member functions. They do not have the implicit this parameter, so they can only access static member variables.

 

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.