C ++ Primer study note _ 22 _ class and data abstraction (8) -- static member variable, static member function, class/object size, _ 22 -- static

Source: Internet
Author: User

C ++ Primer study note _ 22 _ class and data abstraction (8) -- static member variable, static member function, class/object size, _ 22 -- static

I. static

Each static data member is an object associated with the class and is not associated with the object of this class! Non-static data members exist in each object of the class type, and static data members are independent of any object of the class.

The static member function does not have this parameter. It can directly access the static member of the class, but cannot directly use the static member!

1. static member variables

For all objects of a specific type, you may need to access a global variable. For example, count the number of created objects of a certain type.
If we use global variables, the Data encapsulation will be damaged. Generally, the user code can modify this global variable. In this case, the static member of the class can be used to solve this problem.

(1) Definition of static members

Static members must be initialized and defined in the class definition body.

(2) Special integer static const Member

When it is an integer, there are three cases: first, you can declare it here, but you still need to initialize the definition in the class definition outside, because the const must be initialized. Second, if you initialize the definition directly here, you cannot assign values in the class definition body, and the const cannot assign values repeatedly. Third, Initialization is definitely not allowed in the initialization list of the constructor. Note: initialization cannot be performed here when it is not an integer. The integer types include char, short, long, and int (but I tried float, and double is still possible in g ++, so you need to review the information)


[Example]

Class Test {public: Test (): a (0) {} enum {size1 = 100, size2 = 200}; private: const int; // only static int B can be initialized in the constructor initialization list; // define and initialize const static int c in the class implementation file; // and static const int c; same .}; Int Test: B = 0; // The static member variable cannot be initialized in the constructor initialization list because it does not belong to an object. Const int Test: c = 0; // Note: when assigning values to static member variables, you do not need to add the static modifier, but add the const

(3) Advantages of static members:

-- The static member name is in the scope of the class, so it can avoid conflicts with other class members or global object names.

-- Encapsulation can be implemented. Static members can be private members, but global objects cannot.

-- Through the reading program, it is easy to see that static members are associated with specific classes. This visibility clearly shows the programmer's intent.

You can call static members directly from a class through the scope operator, or indirectly through objects, references, or pointers to objects of this type. Static members follow normal public/private access rules
# Include <iostream> using namespace std; class CountedObject {public: CountedObject () {++ count _;}~ CountedObject () {-- count _;} public: static int GetCount (); private: static int count _; // reference declaration of static members}; int CountedObject :: count _ = 0; // definition declaration of static members 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 indirectly through an object <CountedObject: GetCount () <endl; // call CountedObject * co2 = new CountedObject directly from the class through the scope operator; cout <co2-> GetCount () <endl; // call cout indirectly through a pointer to an object of this type <CountedObject: GetCount () <endl; delete co2; cout <CountedObject: GetCount () <endl ;}
Running 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: static member function or non-static member function.


2. static member functions

(1) The static member function does not have an implicit this pointer. The static member is a component of the class but not a component of any object.
(2) non-static member functions can access static members.
(3) static member functions cannot directly access non-static members (in fact, direct access is not allowed, but indirect access is allowed, such as through class pointers or class references)

(4) because the static member is not part of any object, the static member function cannot be declared as const. After all, declaring a member function as const promises not to modify the object to which the function belongs.

(5) Finally, static member functions cannot be declared as virtual functions (described later ).


Ii. Calculation of class/object size

(1) the class size is related to the data member (the empty class size is 1 byte) (2) the class size is irrelevant to the member function.
(3) the class size has nothing to do with static data members.
(4) The impact of virtual functions on the class size will be considered later
(5) The impact of virtual inheritance on the class size will be considered later
[Example]
# Include <iostream> using namespace std; class Test {public: Test (int y): y _ (y ){}~ Test () {} void TestFun () {cout <"x =" <x _ <endl; // OK, non-static member functions can access static member TestStaticFun ();} static void TestStaticFun () {cout <"TestStaticFun... "<endl; // TestFun (); Error, static member functions cannot call non-static member functions // cout <" y = "<y _ <endl; error, static member functions cannot access non-static members} static int x _; // description of the reference of static members int y _; // only related to data members}; int Test :: x _ = 100; // definition of static members int main (void) {cout <sizeof (Test) <endl; return 0 ;}
Running result:
4
Explanation: The above is only related to the data member of the class and defines an int type. Therefore, the size of this class is 4 bytes.


Refer:

C ++ primer version 4
Valid tive C ++ 3rd

Http://blog.csdn.net/zjf280441589/article/details/24838175

Http://blog.csdn.net/jnu_simba/article/details/9236565

C ++ programming specifications

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

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.