Static member variables in C + + classes, static member functions

Source: Internet
Author: User

//static member variables in a class, static member functions#define_crt_secure_no_warnings#include<iostream>using namespacestd;/*A static member function is a variable that belongs to the entire class, static modifiers, belongs to a class, and all objects can be shared. In a static data member function of a class, the property of a variable that cannot invoke a specific object is because the static modified variable is the entire class share, using the member property of an object in a static member function C + + The compiler cannot know which object this member property is, so only static member variables can be used in static member functions*/classpoint{ Public: Point (int_x,int_y) {cout<<"The custom parameter constructor was called 1"<<Endl; X=_x; Y=_y; } Point&pm) {cout<<"I am a custom copy constructor! "<<Endl; }    ~Point () {cout<<"The custom destructor was called 3"<<Endl; }    Static voidGetnum () {cout<< Z <<Endl; }    Static voidSetnum (intc) {Z=C; }Private:    intx; inty; //Static member variables    Static intz;};//static member initialization of a class (correct example)intPoint::z =Ten;voidProtecta () {//Point P1 (); //Warning Warning C4930: "point P1 (void)": The prototype function is not invoked (is it intentionally defined with a variable?) //Analysis: By observing that this line of code C + + compilation does not execute at all, to call the parameterless constructor point P1;//The call copy constructor should use Point P1 (p2); //Point P2 (1,1,2); //Error LNK2001: unresolved external symbol "Private:static int point::z" ([email protected]@@0ha)//This indicates that the static member variable of the class cannot be initialized with the constructor function//static member initialization of a class (error example)//int point::z = 0; //Error C2655: "point::z": defined or re-declared illegal in the current scope//initializing a static member of a class within a function is considered to be a member variable that defines a class//to print the value of a static member variable//method of calling a static member function 1Point::getnum (); //method of calling a static member function 2Point P3 (2,2); P3. Setnum ( One);    Point::getnum (); P3. Getnum ();}voidMain () {protecta (); System ("Pause");}

Static member variables in C + + classes, static member functions

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.