C ++ static data member and static member function

Source: Internet
Author: User

In the process of C ++ object-oriented programming, data between objects is not shared. When designing a class, sometimes data shared between objects is required, in addition to setting the data to be shared as global data or functions, you can also use the static mechanism of c ++.

1. static data member

Class Name {...... Static type description ;...... };

Static data members are shared by all objects, and the memory space occupied by static data members is not allocated because of the generation of an object, nor will they disappear because of the destruction of the object. Like other non-static data members in the class, when it is defined as private, it cannot be accessed by the outside world. However, it can be accessed by any function with the class access permission.

Static data member initialization: A non-static member can be initialized in the constructor, but static cannot be initialized in the constructor. Its initialization can only exist in the global region, to specify the static members of a class, you can use the scope symbol ":" To specify the static members.

Class Name: static data member

That's simple.

When it is declared as public, it can be directly accessed and modifiedDo not use static member functions.

2. static member functions

Class Name {... Static function name (shape parameter) {function body }... };

Static member functions cannot access non-static data members with any permissions. In other words, they can only access static data. It is not easy to call static member functions outside the class.

Object. static member function

This is the practice of common member functions,

Class Name: static member function
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.