C + + static member variables and static member functions

Source: Internet
Author: User

Static member: the member with the Static keyword added before the definition. As follows:

classcrectangle{ Public: Crectangle (); ~Crectangle (); Static voidprinttotal ();Private:    intW, H; Static intNtotalarea; Static intNtotalnumber;}; Crectangle::crectangle (intW_intH_) {W=W_; H=H_; Ntotalnumber++; Ntotalarea+ = w*h;} Crectangle::~Crectangle () {Ntotalnumber--; Ntotalarea-= w*h;}voidcrectangle::P rinttotal () {cout<< Ntotalnumber <<", "<< Ntotalarea <<Endl;}

int crectangle::ntotalnumber = 0;
int crectangle::ntotalarea = 0;
Static member variables must be described once in the file that defines the class
or initialize, otherwise the compilation can pass, the link cannot pass.

int main ()
{
Crectangle R1 (3, 3), R2 (2, 2);
cout << Crectangle::ntotalnumber; Wrong, private
Crectangle::P rinttotal ();
R1. Printtotal ();
return 0;
}

Difference:

1) Normal member variables each object has its own copy, while the static member variable is shared for all objects.

The sizeof operator does not evaluate static member variables.

class CMyClass {    int  n;     Static int s;};

The sizeof (CMyClass) result is 4

2) A normal member function must be specific to an object, and a static member function does not specifically act on an object.

Therefore, static members do not need to be accessible through objects.

How to access static members

1) class Name:: member name

Crectangle::P rinttotal ();

2) object name. Member Name

Crectangle R; R.printtotal ();

3) member name with pointer

Crectangle *p = &r; P->printtotal ();

4) reference. Member name

Crectangle & ref = R; int n = ref.ntotalnumber;

Static member variables are essentially global variables, and even if an object does not exist, the static member variable for the class exists.

A static member function is essentially a global function.

Setting a static member The purpose of this mechanism is to write global variables and functions that are closely related to certain classes into the class, which looks like

A whole, easy to maintain and understand.

Static member Example

Consider a graphics handler that needs to know the total number of rectangles and the total area at any time

You can use global variables to record total and total area

Using static members to encapsulate these two variables into a class is easy to understand and maintain. (e.g. code at the beginning of the article)

Precautions

1) in a static member function, you cannot access a non-static member variable or call a non-static member function.

Defects in this Crectangle class

When using the Crectangle class, the copy constructor is sometimes called to generate a temporary hidden Crectangle object

1) When invoking a function that takes the Crectangle class object as a parameter

2) when calling a function that takes the Crectangle class object as the return value

Temporary objects call Destructors when they die, reducing the values of ntotalnumber and Ntotalarea, but these temporary

Objects are generated without increasing the values of Ntotalnumber and Ntotalarea.

Workaround: Write a copy constructor for the Crectangle class. As follows

Crectangle::crectangle (Crectangle & R) {    = R.W;     = R.h;    Ntotalnumber++    ; + = w*h;}

C + + static member variables and static member functions

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.