Class static members and static member functions

Source: Internet
Author: User

Static declared members or functions are shared by all objects of the class and do not save a copy in each class. They belong to the class itself, even if no object is instantiated, it can still be accessed or called. 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>/* class static member */
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> # include <iostream>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> using namespace std;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif" align = "top"/> class {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> static int a; // declare static members
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> int B;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> public:
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src = "http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif" align = "top"/> void set (int I, int j) {a = I; /* modify the value of static member a */B = j ;}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> void show ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> };
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> int A: a; // definition section
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> void A: show ()
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> cout <" static member a: "<a <endl;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> cout <" non-static member B: "<B <endl;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> int main (int argc, char ** argv)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> A aa1, aa2;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> aa1.set );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> aa1.show ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src = "http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif" align = "top"/> cout <"++ + "<endl;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> aa2.set );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> aa2.show ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src = "http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif" align = "top"/> cout <"++ + "<endl;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> aa1.show (); // any modifications to static member a can be reflected in other objects
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> return 0;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>} simply add a keyword static before the normal way of defining it to become a static member. However, it is worth noting how it is initialized because it is independent of class objects and cannot be initialized in constructors, because it should exist before all objects are instantiated (even if no constructor is called ). where is the initialization. In fact, you can find out where the class member functions are defined, much like declaring them externally. Note that there is no static. 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src = "http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif" align = "top"/> int A: a; the declaration below is public and A value of 650 is initialized) this. width = 650; "onclick = 'window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>/* class static member */
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> # include <iostream>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> using namespace std;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif" align = "top"/> class {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> int B;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> public:
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> static int a; // declare static members
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src = "http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif" align = "top"/> void set (int I, int j) {a = I; /* modify the value of static member a */B = j ;}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> void show ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> };
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> int A: a = 123; // definition section
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> void A: show ()
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> cout <" static member a: "<a <endl;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> cout <" non-static member B: "<B <endl;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>}
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> int main (int argc, char ** argv)
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> {
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> cout <A: a <endl;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> A aa1, aa2;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> aa1.set );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> aa1.show ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src = "http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif" align = "top"/> cout <"++ + "<endl;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> aa2.set );
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> aa2.show ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src = "http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif" align = "top"/> cout <"++ + "<endl;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> aa1.show (); // any modifications to static member a can be reflected in other objects
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src = "http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif" align = "top"/> cout <"++ + "<endl;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> A: a = 100; // public can be accessed directly
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> aa1.show ();
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/> return 0;
650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'src =" http://www.bkjia.com/uploads/allimg/131228/1ZJM927-0.gif "align =" top "/>}
: 650) this. width = 650; "onclick = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) 'alt =" "src =" http://www.bkjia.com/uploads/allimg/131228/1ZJL409-79.gif "border =" 0 "/>

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.