Static member variable. xml

Source: Internet
Author: User
Pre {Line-Height: 1; color: # 1e1e1e; Background-color: # f0f0f0; font-size: 16px ;}. sysfunc {color: #627cf6; font-style: italic; font-weight: bold ;}. selffuc {color: #800080 ;}. bool {color: # d2576f ;}. condition {color: #000080; font-weight: bold ;}. key {color: #000080 ;}. vaR {color: #800000; font-style: italic ;}. digit {color: # ff00ff; font-weight: bold ;}. includepre {color: # 1e1e1e ;}. operator? {Color: #008000; font-weight: bold ;}

Static member variable

1. static member functions can only access static variables because static variables do not include the this pointer.

2 static member variables ?? It is not only shared by all objects of the class, but also shared by objects of the derived class.

Example:

?class?base{public:?????????static?int?num;};?int?base::num=0;?class?derived:public?base{};??????????base?a;?????????derived?b;?????????a.num++;?????????cout<<a.num<<endl;??????????b.num++;?????????cout<<b.num<<endl;?

3? A static member of a class can be an optional parameter of a member function, but a common member cannot.

Example:

Class? Base {? Public :????????? Static? Int? Num ;?????????? Int? VaR ;?????????? Void? F1 (Int? I = num );?? //? Correct ????????? Void? F2 (Int? I = var );?? //? Error };?

4? A static member of a class can be of the class type, but a common member cannot. A common member can only be declared as a pointer or reference of the class type.

? Class? Base {? Public :????????? Static? Base? Num ;?????????? Base? VaR ;? //? Error ?????????? Base? * Pbase ;?????????? Base? & Mybase ;};

Used in this article? Calligraphy and painting novel software? Released, the content has nothing to do with the software, and the painting and calligraphy novel software? More comfortable reading, more comfortable writing, and easier Publishing.

Static member variable. xml

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.