Static data members and static member functions for C + + classes

Source: Internet
Author: User

Static data members

• Use the keyword static declaration

• When declaring a class's data member as static, no matter how many classes of objects are created, the static member has only one copy

• Shared in all objects of a class with static lifetime

• If no other initialization statement exists, all static data members are initialized to zero when the first object is created

• Define and initialize outside the class, using the scope resolution operator (::) to indicate the owning class

Example:

#include <iostream>using namespace Std;class Box {public:static int count;    If the static data member is declared in the private section, the box can only be processed by a static member function (double L = 2.0, double b = 2.0, double h = 2.0) {cout << "one constructor wa s called. "<< endl;length = l, Width = b, height = h;count++;//Each object is created with 1}double Volume () {return length * Width * h Eight;} ~box () {count--;} Private:double length, width, height;};/ /Initialize the static member of the class Box int box::count = 0;int Main (void) {Box Box1 (3.3, 1.2, 1.5); Box Box2 (8.5, 6.0, 2.0); cout << "Total objects:" << box::count << endl;//number of output objects return 0;

Static member functions

If you declare a member function as static, you can separate the function from any particular object of the class.

• Can also be called if the class object does not exist, using the class name plus the scope resolution operator :: You can access

• Static member functions can access only static member data, other static member functions, and other functions outside the class

• A static member function has a class scope and cannot access the this pointer of a class, you can use a static member function to determine whether certain objects of a class have been created

• Use static member functions to access non-static members through the object

Example:

#include <iostream>using namespace Std;class Box {public:static int count; Box (double L = 2.0, double b = 2.0, double h = 2.0) {cout << "one constructor was called." << endl;length = L, width = b, height = h;count++;} Double Volume () {return length * width * height;} static int GetCount () {///static member function return count;} Private:double length, width, height;}; int box::count = 0;int Main (void) {//Total number of Output objects before object creation cout << inital Stage count: << box::getcount () << Endl Box Box1 (3.3, 1.2, 1.5); Box Box2 (8.5, 6.0, 2.0);//The total number of output objects after the object is created cout << Final Stage Count: << box::getcount () << Endl;return 0;}

Note:

The difference between a static member function and a normal member function:

• Static member function does not have this pointer, only static members (including static member variables and static member functions) can be accessed

• The normal member function has this pointer and can access any member of the class, while the static member function does not have the this pointer

Using static members to understand the invocation of constructs and destructors

#include <iostream>using namespace Std;class a {friend class b;//class B is a friend of class A public:static int value;static int num; A (int x, int y) {xp = x, YP = y;value++;cout << "call construct:" << value << Endl;} void Displaya () {cout << xp << "," << yp << Endl;} ~a () {num++;cout << "call destructor:" << num << Endl;} Private:int XP, YP;}; Class B {public:b (int x1, int x2): mpt1 (x1 + 2, x2-2), Mpt2 (x1, x2) {cout << "call construct \ n"),//mpt is an object of Class A, there are several MPT, the operation of Class A Executes several times}void set (int m, int n), void Displayb (), ~b () {cout << "call destructor \ n";//destructors are called before the class ends, the class request space is freed when classes end} Private:a MPT1, MP t2;//declares an object of Class A as a private data member of class B};int a::value = 0;int A::num = 0;void b::set (int m, int n) {mpt1.xp = m * 2, MPT1.YP = N/2;} void B::d isplayb () {Mpt1.displaya ();} int main () {B P (ten); cout << "Hello world!" << Endl; B Displayb ();    With friend, make Class B output the private data member of Class A return 0;}

Related articles:

Use of static and constant members of C + +

C + + Review Essentials Summary of the five static member variables and 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.