First, static
each static A data member is an object associated with a class and is not associated with an object of that class ! The non-static data member exists in each object of the class type, and the static data member is independent of any object of that class.
Staticmember function does not have Thisparameter, which can be accessed directly by the owning class Staticmember, but cannot be used directlyStatic Members !
1. Static member Variable
For a particular type of whole object, it may sometimes be necessary to access a global variable. For example, count the number of objects that have been created for a certain type.
If we use a global variable to break the encapsulation of the data, the general user code can modify the global variable, you can use the static statics members of the class to solve the problem.
(1), definition of static member
Static members need to be initialized and defined outside the class definition body
(2), special integer static const member
is an integral type, has three cases: first, Note: This cannot be initialized when it is non-integral, integral types include char, short, long, int (but I tried float, and double in g++ still can, so need to re-textual) Span style= "BACKGROUND-COLOR:INHERIT; Color:red ">
Example
Class Test{public: Test (): A (0) {} enum {size1 = +, Size2 = $};p rivate: const int A; static int B can only be initialized in the constructor initialization list ; Defines and initializes the const static int C in the class's implementation file;
(3), static member Advantages:
< Span style= "Background-color:inherit" >--static The member's name is Span style= "BACKGROUND-COLOR:INHERIT; Color:rgb (255,51,51) "> In the scope of the class So you can .
< Span style= "Background-color:inherit" >-- can implement encapsulation . static Members can be private members < Span lang= "en-US" style= "Background-color:inherit", Global objects are not allowed.
--Easy to see through the Reading program Static a member is associated with a specific class . This visibility can clearly show the programmer's intentions.
a static member can be called directly from a class through a scope operator , or through an object , reference Or a pointer to an object of the class type is called indirectly. Staticmembers follow a normal public/Private access Rules
#include <iostream>using namespace Std;class countedobject{public: countedobject () {++count_;} ~countedobject () {--count_;} Public: static int GetCount ();p rivate: static int count_; Declarative declarations of static members};int Countedobject::count_ = 0; The definition declaration of a static member int Countedobject::getcount () { return count_;} int main (void) { cout << countedobject::getcount () << Endl; Countedobject Co1; cout<<countedobject::count_<<endl; Error cout << co1. GetCount () << Endl; Call cout << countedobject::getcount () << Endl through objects indirectly, or directly from a class by a scope operator countedobject *CO2 = new Countedobject; cout << co2->getcount () << endl;//indirectly calls cout << countedobject::getcount () by pointers to objects of that class type < < Endl; Delete CO2; cout << countedobject::getcount () << Endl;}
Operation Result:
0
1
1
2
2
1
Explanation: The above program defines a static member variable and a static member function, which can be accessed through the class name:: Access to the static member function , or through a non-/static member function.
2. Static member function
(1) Static member function has no implied this pointer,static member is part of class but not part of any object
(2) non-static member functions can access static members
(3) Static member functions do not have direct access to non-static members (in fact direct access is not possible, indirect access is possible, e.g. through class pointers or class references)
(4)becauseStaticmembers are not part of any object,soStaticmember functions cannot be declared asConst. After all,declare the member function asConstis to promise not to modify the object to which the function belongs.
(5) finally , the static member function cannot be declared as a virtual function (described later).
Second, class/object size calculation
(1) The size of the class is related to the data member (the empty class size is 1 bytes)(2) the size of the class is independent of the member function
(3) The size of the class is independent of the static data member
(4) The effect of virtual function on the size of the class, and subsequent consideration
(5) The effect of virtual inheritance on the size of the class, and subsequent consideration
Example
#include <iostream>using namespace Std;class test{public: Test (int y): Y_ (y) {} ~test () {} void Testf Un () { cout << "x=" << x_ << Endl;//ok, non-static member functions can access static members Teststaticfun (); } static void Teststaticfun () { cout << "Teststaticfun ..." << Endl; Testfun (); Error, static member function cannot call non-static member function //cout<< "y=" <<y_<<endl; Error, static member function cannot access non-static member } static int x_; A descriptive description of the static member int y_; Only related to data members};int test::x_ = +; The definition of a static member int main (void) { cout << sizeof (Test) << Endl; return 0;}
Operation Result:
4
Explanation: The above is only related to the data members of the class and defines an int type, so the class size is 4 bytes.
Reference:
C + + Primer Fourth Edition
Effective C + + 3rd
http://blog.csdn.net/zjf280441589/article/details/24838175
http://blog.csdn.net/jnu_simba/article/details/9236565
C + + Programming specification
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
C + + Primer Learning Notes _22_ class and Data Abstraction (8)--static member variable, static member function, class/object size