The difference between static variables and ordinary variables in C + + classes

Source: Internet
Author: User

Static variables:

1. Static variables will be programmed into the EXE, from the start to the end of the program, it has been there;

2. The initialization value of the static variable is 0;

3. Global variables are static variables by default;

4. A static variable is added to the function variable in the class, except that it is only used within the function by the language;

5. In the MFC dialog box, if a variable and dialog box binding, then the variable cannot be declared as a static variable, the function is the same, because the declaration as a static variable can not accept the value of the dialog box at any time, mainly because the value of the static variable can not be modified after setting;

6. You can use the class name + static member name to access this static member because static members exist in memory, non-static members need to be instantiated to allocate memory, so static members cannot access non-static members because static members exist in memory, so non-static members can directly access static members in the class;

7. Add static to the inner variable of the class, declare this variable as a static member variable, regardless of how many objects of the class are defined, and static data members have only one copy in the program, accessed by all objects of that type. That is, a static data member is common to all objects of that class. For multiple objects of this class, static data members are allocated only once for all objects to be shared. Therefore, the value of the static data member is the same for each object, and its value can be updated;

8. Static variables in classes are initialized in the form of:< data type >< class name >::< static data member > = < value >;

9. Static data members and ordinary data members comply with public, private, protected access rules, if the static data members access permission, that is, the public member, you can in the program, in the format mentioned above to refer to static data members;

10. There are two advantages to using static data members compared to global variables:

(1) static data members do not enter the program's global namespace, so there is no possibility of conflict with other global names in the program;

(2) Information hiding can be realized. Static data members can make private members, while global variables cannot.

11. For a static member function inside a class that does not have the this pointer, and the non-static function has a this pointer to the object itself of the class, the static member function cannot access the non-static data member belonging to the class object and cannot access the non-static member function, which can only invoke the rest of the static member functions.

Non-static variables:

1. A non-static variable is simply not included in the stack when the function is called, and the variable is destroyed after the function call ends;

2. The value of a non-static variable can be changed.

3. When a class is instantiated, it can be accessed by instantiating the class name, the lifetime of the non-static member is determined by the lifetime of the class, and the static member does not have the concept of a lifetime, because static members always reside in memory.

The difference between static variables and ordinary variables in C + + classes

Related Article

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.