10 In the C ++ notes series-static members

Source: Internet
Author: User

1. Static members
(1) modified by the keyword static
The static variable definition statement is executed at the compilation stage and no longer executed during the running process.
(2) classification: static data members and static member functions.
(3) When a static member is used, all objects of the class share members, rather than members of an object.
2. Use of static members
(1) Add the static keyword before the definition description. For example:
Static int x;
(2) static data members must be initialized and the initialization must be completed outside the class.
(3) static data member Initialization
Data Type Class Name: static data member name = value; // note that no static data appears here
(4) object names cannot be used to use static data members. Again, it is emphasized that static data members belong to the whole class of public, rather than an object.
Object Name. static member name // Error
(5) correct usage
Class Name: static data member
(6) If the access permissions of static data members are permitted, they can be accessed outside the class.
3. Example of static data members
# Include

Class Tc
{
Int I;
Static int k;
Public:
Tc ()
{
I = 0;
I ++;
K ++;
}
Void display ()
{
Cout <"I =" <

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.