Static data members in C + +

Source: Internet
Author: User

A static member of a class cannot be initialized by the constructor of a class. Because even if no object of the class exists, the static members of the class still exist and can be used. The static members of a class also cannot access non-static members of any class.

Both the class name and the class object can call static data members directly. Because a static data member already exists before the constructor is executed.  

However, if the static member is integer or enumerated const, you can initialize it in the class declaration !  

The static decorated function indicates that the function belongs to a class, not to an object, and does not have the this pointer.

A member variable in a C + + class is declared as static, whichmeans that it is shared by all instances of the class , that is, when an instance of a class modifies the static member variable, This means that no matter how many objects are created, thestatic modified variable occupies only one piece of memory . Its modified value is seen by all other instances of the class, and static member functions of the class can access only static members (variables or functions). Static is a global variable that adds access control and is not inherited.  

Static data members in C + +

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.