One. Review of member variables
1. The public member variable can be accessed through the object name.
2. Each object can have a member variable that belongs only to itself
3. Member variables cannot be shared between objects.
Two. Static members of a class
1. Static member variables and static member functions can be defined in C + +
2. Static members are owned by the entire class and do not need to be dependent on any objects.
3. public static members can be accessed directly through the class name
4. public static members can be accessed by object name
5. Static member functions can access static member variables directly.
Three. Definition of static member variable
1. Modify directly with the Static keyword when defining
2. Static member variables do not depend on any objects, and you need to allocate space separately outside the class.
3. Syntax rules: Type classname::varname:
#include <cstdlib>#include<iostream>using namespacestd;classtest{Private: Static intCI; Public: Static intGeti () {returnCI; } Static voidSetI (inti) {ci=i; }};intTest::ci =0;intMainintargcChar*argv[]) {Test::seti (5); cout<<"Test::ci ="<< Test::geti () <<"\ n"; cout<<"Press the ENTER key to continue ..."; Cin.Get(); returnexit_success;
5. From the perspective of the namespace
A. Static members of a class are just global variables and global functions in the class namespace
B. The difference is that a class can restrict access to static members, and namespaces do not
6. From an object-oriented perspective
A. The static members of a class belong to the class concept itself
B. All objects of the class share the same static members
Four. Definition of a static member function
1. Modify directly with the Static keyword when defining
2. The remainder is the same as the normal member function definition
Five. What is the difference between a static member function and an ordinary member function?
member variables and member functions in C + + class objects are stored separately
1. Member variables
Normal member variables: stored in an object, with the same memory layout and byte alignment as the struct variable
Static member variables: stored in the global data area
2. member functions
stored in the code snippet
Six.
#include <stdio.h>classtest{inti; intJ; intK; Static intC; Public: Test (intIintJintk) { This->i =i; This->j =J; This->k =K; } voidprint () {printf ("Object Address:%08x\n", This); printf ("&c =%08x, c =%d\n", &c, c); printf ("&i =%08x, i =%d\n", &I, i); printf ("&j =%08x, j =%d\n", &J, J); printf ("&k =%08x, k =%d\n", &K, k); }};intTest::c;intMain () {Test T1 (0,1,2); Test T2 (3,4,5); printf ("T1 Address:%08x\n", &t1); T1.print (); printf ("T2 Address:%08x\n", &T2); T2.print (); printf ("Press any key to continue ..."); GetChar (); return 0;}
Six. Summary
The 1.c++ class can contain static members that belong to the class concept
2. Static member variables allocate space in the global data area
3. Static member function does not contain hidden this pointer
4. A static member can be accessed directly through the class name
Static member _12 for C + + classes