C + + Static keyword Summary

Source: Internet
Author: User
Tags class definition

1. Static Global variables

By adding the static keyword between global variables, it is defined as a static global variable.

Features: allocating memory in the global data area

Uninitialized is automatically initialized to 0

Visible within the file that declares it, files outside the declaration are not visible, and other files can use the same name variable

2. Static local Variables

Add the static keyword to the local variable, which is defined as a static local variable

Features: allocating memory in the global data area, but scoped to local scope

It is typically initialized at declaration time and can only be initialized once, and is not initialized when the function is called at a later time.

If initialization is not displayed, initialize to 0

3. Static functions

With the static keyword before the function return type, it is defined as a static function

Features: Visible only in the file that declares it, not in other files

Functions with the same name can be found in other files

4. Static data members

The static keyword is defined before the class data member declaration, and is

Features: Regardless of the number of class objects, static data members have only one copy, shared access by all objects, only one memory allocated once, the value can be updated

Static data members are stored in the global data area and are allocated space when defined, so they cannot be defined at declaration time (Assignment) and can be initialized when the first object is declared, in the form

The data type >< class name >::< the static data member name >=< value, which can be placed in the class definition so that the class declares the object and initializes

Static data members adhere to Public,private,protect access rules

Information hiding is possible compared to global variables, and there is no naming conflict because it does not enter the global namespace

5. Static member functions

Adding the Static keyword before the class member function is defined as a static member function

Features: Service for a class instead of an object, is an internal implementation of the class and is part of the class definition

Because the static member function does not contact the class object, there is no default this pointer, so non-static data members cannot be accessed, and non-static member functions cannot be accessed

In addition to using the member access operator (. and), you can also access static member functions directly, in the form of the class name >::< static member function name (parameter table)

C + + Static keyword Summary

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.