C ++ class static data members and class static member functions

Source: Internet
Author: User

Before proceeding to this Chapter, if we want to share a certain data within a certain range, we will set up a global object, but the object-oriented program is composed of objects, how can we share data within the scope of the class?

This is the focus of this chapter:

Declared static class members or member functions can be shared within the scope of the class.We name such a memberStatic member and static member functions.

Below we use several instances to illustrate this problem. The class members need to be protected. Generally, in order not to violate the class encapsulation feature, we set the class member to protected (protection status) but in order to simplify the code and make the problem to be explained more intuitive and easy to understand, we set it to public here.

The following program is used as an example to simulate access. In the program, each time an object is created, the static class member we set will automatically add one. The Code is as follows:


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
Using namespace std;

Class Internet
{
Public:
Internet (char * name, char * address)
{
Strcpy (Internet: name, name );
Strcpy (Internet: address, address );
Count ++;
}
Static void Internet: SC () // static member function
{
Cout <count <endl;
}
Internet & Rq ();
Public:
Char name [20];
Char address [20];
Static int count; // if it is written as static int count = 0, it is incorrect.
};

Internet & Internet: Rq () // return the referenced member function
{
Return * this;
}

Int Internet: count = 0; // initialization of static members
Void vist ()
{
Internet a1 ("China Software Development Lab", "www.cndev-lab.com ");
Internet a2 ("China Software Development Lab", "www.cndev-lab.com ");
}
Void fn (Internet & s)
{
Cout <s. Rq (). count;
}
Void main ()
{
Cout <Internet: count <endl; // output of static member values
Vist ();
Internet: SC (); // call a static member function
Internet B ("China Software Development Lab", "www.cndev-lab.com ");
Internet: SC ();
Fn (B );
Cin. get ();
}


The above Code uses several common methods to create an object. When a new object is created and its constructor is called, cout, a static member, runs the Add 1 operation, static member initialization should be performed before the main function is called and cannot appear in the class declaration. We can observe the running process and find that, the status of the static member count is not redefined with the creation of a new object. As a result, we understand that the static member of the class belongs to the class rather than to which object, so the use of static members should beClass Name and domain differentiation plus Member nameIn the above Code, Internet: count is used. Although we can still use object names to add some operation symbols and member names, it is not recommended, static State class members belong to a class rather than a specific object.

Static member functions are similar to the use of static members and are also irrelevant to objects. The call method isClass Name and domain differentiation plus member function nameIn the above Code, the static member functions are Internet: SC (); and are irrelevant to objects. Therefore, normal members of the class cannot be directly operated on.

If the above static void Internet: SC () is changed

Static void Internet: SC () // static member function
{
Cout <name <endl; // Error
Cout <count <endl;
}



The difference between a static member function and a common member function lies in the lack of this pointer. Without this pointer, you naturally cannot know which object name is a member.

Based on the characteristics of static class members, we can briefly summarize the following points:

1. Used to save the number of objects.

2. As a flag, Mark whether some actions occur, such as the file opening status, printer usage status, and so on.

3. Store the memory address of the first or last member of the linked list.

In order to do some necessary exercises and thoroughly understand the significance of the existence of static objects, we described a Linear Linked List in the form of classes based on the previous structure tutorial, the Code is as follows:


// Program Author: Guan Ning
// Site: www.cndev-lab.com
// All the manuscripts are copyrighted. If you want to reprint them, be sure to use the famous source and author.

# Include <iostream>
Using namespace std;

Class Student
{
Public:
Student (char * name );
~ Student ();
Public:
Char name [30];
Student * next;
Static Student * point;
};

Student: Student (char * name)
{
Strcpy (Student: name, name );
This-> next = point;
Point = this;
}

Student ::~ Student () // The Node detachment process
{
Cout <"structure:" <name <endl;

If (point = this)
{
Point = this-> next;
Cin. get ();
Return;
}
For (Student * ps = point; ps = ps-> next)
{
If (ps-> next = this)
{
Cout <ps-> next <"|" <this-> next <endl;
Ps-> next = next; // = next can also be written as this-> next;
Cin. get ();
Return;
}
}
Cin. get ();
}

Student * Student: point = NULL;
Void main ()
{
Student * c = new Student ("marry ");
Student a ("colin ");
Student B ("jamesji ");
Delete c;
Student * fp = Student: point;
While (fp! = NULL)
{
Cout <fp-> name <endl;
Fp = fp-> next;
}
Cin. get ();
}


From the code above, a linked list entry global pointer required by simple structured programming is replaced by the static member pointer of the class here (the static member of the class can completely replace the global variable ), the focus of this example is to observe the structure order of class members.Structure orderYou can use destructor to perform node leeching.

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.