Static member variables for class 25th

Source: Internet
Author: User

1. Review of member variables

(1) Access to public member variables by object name

(2) The member variables of each object are exclusive

(3) member variables cannot be shared between objects

2. static member variables

(1) Static member variables in C + +

① static member variables belong to the entire class all

② the lifetime of a static member function variable is not dependent on any object

③ can access public static member variables directly through the object name or through the class name

static member variables for all objects shared classes

(2) Characteristics of static member variables

① is modified directly by the static keyword when defined

② static member variables are located inside the program within the global data area , so you need to allocate space outside of the class . (Because objects in new or stack will only allocate space for non-static members, not static members, you must allocate space for them separately outside of the class)

③ The syntax rules for allocating space for static member variables:

Type classname::varname = value; // to be placed outside the function as if you were defining a global variable

The use of static member variables for "programming experiments"

#include <stdio.h>classtest{Private:    Static intCcount; Public: Test () {ccount++;} ~test () {--Ccount;} intGetCount () {returnccount;}};//You must allocate space to static member variables outside of the class before useintTest::ccount =0;//indicates that you want to be in the global zone and can only allocate space in the global zone and initialize to 0Test gtest;//ccount increased by 1intMain () {//int test::ccount = 0; error, attempting to allocate space for static members hereTest T1;//ccount increased by 1Test T2;//ccount increased by 1printf ("sizeof (Test) =%d, sizeof (t1) =%d\n",                sizeof(Test),sizeof(t1));//equals 1, indicating that ccount is allocating space in the global zone,//the variable does not belong to an object itself. printf ("count =%d\n", Gtest.getcount ());//3, accessing static member variables by object nameprintf"count =%d\n", T1.getcount ()); printf ("count =%d\n", T2.getcount ()); Test* pt =NewTest ();//ccount increased by 1printf ("count =%d\n", Pt->getcount ());//4    DeletePt//ccount minus 1 .printf ("count =%d\n", Pt->getcount ());//3//printf ("Count =%d\n", test::ccount);//error, only public static member variables can be accessed through the class name    return 0;}

3. Summary

(1) A static member variable can be defined in a class by using the static keyword

(2) static member variable belongs to class all

(3) A static member variable can be accessed by each object

(4) Static member variable allocates space in global data area

(5) The lifetime of the static member variable is the program run time

Static member variables for class 25th

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.