The mutual exchange between static member functions and non-static member functions of Linux C + + classes __java

Source: Internet
Author: User
Preface:The member variables between objects and objects are independent of each other. To share data, you need to use static members and static methods. As long as you declare static member variables in a class, you can allocate space for static member variables, even if you do not define them, and you can use static member variables. (because static member variables have been allocated memory space before the object was created) static member variables, while in the class, but it does not allocate space with the object's creation, nor is it released with the object's revocation (the average member allocates space when the object is set up and releases it when the object is undone). A static member variable allocates space at program compile time and frees space at the end of the program. The definition and declaration of static members are added to a key static. Static members can be used by double colons, that is, the < class name >::< static member name >. Initialization of static member variables is done outside the class. The initialization format is as follows: Data type class Name:: static member Variable name = initial value; You cannot initialize a static member variable with a parameter initialization table. Static member variables can be referenced either through the class name or by the object name. The difference between a normal member function and a static member function is that the normal member function passes a this pointer in a hidden way when the parameter is passed. The this pointer is used to determine which object the calling class produces;   However, static member functions do not have the this pointer, and do not know which object to access the data, so in the program can not use static member functions to access the normal variables in the class. Here are a few examples to summarize the rules for using static member variables and static member functions. call static member functions and Non-static member functions by class nameExample one: calling static member functions and Non-static member functions through class names class point{public:void init () {} static void output () {}};
void Main () {point::init (); Point::output (); }
Compilation Error: Error 1 C2352: "Point::init": illegal invocation of non-static member function conclusion one: non-static member functions of a class cannot be invoked by class name
second, static member functions and non-static member functions are invoked through the object of the classExample two: Static member functions are invoked through the object of the class and Non-static member functions class point{public:void init () {} {}.
void Main () {point pt; Pt.init (); Pt.output ();} compilation passed. Conclusion Two: The object of class can use static member function and Non-static member function. Use a non-static member of a class in a static member function of a classExample three: Using a non-static member of a class #include <iostream> using namespace std in a static member function of a class;
Class point{Public:void init () {} static void output () {cout << "m_x=" << m_x << Endl;} private:int m_x; };
void Main () {point pt; Pt.output ();} Compile Error: IntelliSense: non-static member references must be relative to a particular object because static member functions belong to the entire class, and space is allocated before the class instantiates the object. A non-static member of a class must have the memory space after the class has instantiated the object, so the call will go wrong, just like not declaring a variable but using it in advance. Conclusion Three: Non-static members cannot be referenced in static member functions. use static members of a class in a Non-static member function of a classExample four: Using static members of a class #include <iostream> using namespace std in non-static member functions of a class;
Class point{Public:void init () {output ();} static void Output () {} private:int m_x;
void Main () {point pt; Pt.init ();} compiled through. Conclusion four: Static member functions can be invoked by Non-static members of the class, but not vice versa. use static member variables of classesExample five: Using static member variables of classes #include <iostream> using namespace std;
Class point{Public:point () {m_npointcount++} ~point () {m_npointcount++;} static void Output () {cout << "M_npo Intcount= "<< m_npointcount << Endl; } private:static int m_npointcount; };
void Main () {point pt; Pt.output ();} Link Error: Error LNK2001: unresolved external symbol "Private:static int point::m_npointcount" (? M_npoint count@point@@0ha) This is because the member variables of a class must be initialized before they can be used. Change to the following code: #include <iostream> using namespace std;
Class point{Public:point () {m_npointcount++} ~point () {m_npointcount++;} static void Output () {cout << "M_npo Intcount= "<< m_npointcount << Endl; } private:static int m_npointcount; };
Static member variables are initialized outside of the class without the static keyword int point::m_npointcount = 0; void Main () {point pt; Pt.output ();}
Run Result: Conclusion Five: Static member variables of class must be initialized and reused.
Vi. methods of static member functions accessing Non-static member functionsStatic member functions can access static member variables, global variables, and their own function parameters. So, you can pass the object as the formal parameter of the static function.
static void A::instance (A * const PA);

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.