Static members and static member functions for C + + classes

Source: Internet
Author: User

  • The member variables between the object and the object 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 the object, and you can use static member variables. ( because static member variables have been allocated memory space before the object is created )
  • A static member variable, although in a class, is not allocated space with the creation of an object, nor is it disposed with the object's revocation (the general member allocates space when the object is established and is freed when the object is revoked). a static member variable allocates space at the time of program compilation, and frees space at the end of the program.
  • Static members are defined and declared with a key static. Static members can be used by double colons, that is, < class name >::< static member name >.
  • Initializes a static member variable to be performed outside of the class. the format of the initialization is as follows: Data type class Name:: static member Variable name = initial value ;
  • You cannot initialize a static member variable with the parameter initialization table.
  • Static member variables can be referenced either by the class name or by the object name.
  • The difference between a normal member function and a static member function is that the compiler will pass a this pointer over the normal member function when the parameter is passed. The this pointer determines which object is produced by the calling class; However, static member functions do not have this pointer and do not know which object should be accessed, so a static member function can not be used in a program to access ordinary variables in a class .

Here are a few examples to summarize the rules for using static member variables and static member functions.

  I. Calling static member functions and non-static member functions by class name

1 //Example one: calling static member functions and non-static member functions by class name2 classpoint{3  Public:4     voidInit ()5     {}6 7     Static voidoutput ()8     {}9 };Ten  One voidMain () A { - point::init (); - point::output (); the}

  Compilation Error: Error 1: "Point::init": illegal call of non-static member function C2352

  Conclusion: A non-static member function of a class cannot be invoked through the class name

  Second, call static member functions and non-static member functions through the object of the class

1 //example two: calling static member functions and non-static member functions through objects of a class2 classpoint{3  Public:4     voidInit ()5     {6     }7 8     Static voidoutput ()9     {}Ten }; One  A voidMain () - { - Point pt; the pt.init (); - pt.output (); -}

  Compiled by.

  Conclusion Two: Objects of a class can use static member functions and non-static member functions.

  Iii. using non-static members of a class in a static member function of a class

1 //example three: Using a non-static member of a class in a static member function of a class2#include <iostream>3 using namespacestd;4 5 classpoint{6  Public:7     voidInit ()8     {9     }Ten     Static voidoutput () One     { Acout <<"m_x="<< m_x <<Endl; -     } - Private: the     intm_x; - }; - voidMain () - { + Point pt; - pt.output (); +}

  Compilation error: IntelliSense: Non-static member reference must be relative to a specific object

  Because static member functions belong to the entire class, the space is allocated before the class instantiates the object, and the non-static members of the class must have memory space after the class instantiates the object, so the call will go wrong, just as it does without declaring a variable but using it in advance.

  Conclusion three: Static member functions cannot refer to non-static members.

  Iv. using static members of a class in a non-static member function of a class

1 //example four: Using static members of a class in a non-static member function of a class2#include <iostream>3 using namespacestd;4 5 classpoint{6  Public:7     voidInit ()8     {9 output ();Ten     } One     Static voidoutput () A     { -     } - Private: the     intm_x; - }; - voidMain () - { + Point pt; - pt.init (); +}

  Compiled by.

  Conclusion Four: Non-static members of a class can call static member functions, but not vice versa.

  V. Using static member variables of a class

1 //example five: Using static member variables of a class2#include <iostream>3 using namespacestd;4 5 classpoint{6  Public:7 Point ()8     {9m_npointcount++;Ten     } One~Point () A     { -m_npointcount++; -     } the     Static voidoutput () -     { -cout <<"m_npointcount="<< M_npointcount <<Endl; -     } + Private: -     Static  intM_npointcount; + }; A  at voidMain () - { - Point pt; - pt.output (); -}

  

  Link Error: Error LNK2001: unresolved external symbol "Private:static int point::m_npointcount" ([email protected]@@0ha)

This is because the member variables of the class must be initialized before they are used.

  Change to the following code:

1#include <iostream>2 using namespacestd;3 4 classpoint{5  Public:6 Point ()7     {8m_npointcount++;9     }Ten~Point () One     { Am_npointcount++; -     } -     Static voidoutput () the     { -cout <<"m_npointcount="<< M_npointcount <<Endl; -     } - Private: +     Static  intM_npointcount; - }; +  A //initialize static member variables outside of class without the static keyword at intPoint::m_npointcount =0; - voidMain () - { - Point pt; - pt.output (); -}

  Operation Result:

  

  Conclusion Five: Static member variables of a class must first be initialized and reused.

  

  

Static members and static member functions for C + + classes

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.