Learning how to use C ++ objects from scratch (1): static member variable, static member function, class/object size

Source: Internet
Author: User

I. 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 broken. Generally, the user code can modify this global variable. In this case, we can use static members of the class to solve this problem.
Non-static data members exist in each object of the class type. static data members are independent of any object of the class. They are objects associated with the class and are not associated with the class object.

(1) Definition of static members

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

(2) Special integer static const Member

An integer static const member can be initialized in the class definition body, which can be defined outside the class body.

(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, but global objects cannot.

The reader can easily see that static members are associated with a class, which clearly reflects the programmer's intent.

C ++ code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Ifndef _ counted_object_h _
# DEFINE _ counted_object_h _

Class countedobject
{
Public:
Countedobject ();
~ Countedobject ();
Public:
Static int getcount ();
PRIVATE:
Static int count _; // declaration of reference of static members
};

# Endif/_ counted_object_h _

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Include "countedobject. H"

Int countedobject: Count _ = 0; // definition declaration of static members

Countedobject: countedobject ()
{
+ + Count _;
}

Countedobject ::~ Countedobject ()
{
-- Count _;
}

Int countedobject: getcount ()
{
Return count _;
}

C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Include "countedobject. H"
# Include <iostream>
Using namespace STD;

Int main (void)
{
// Cout <countedobject: Count _ <Endl;
Cout <countedobject: getcount () <Endl;
Countedobject co1;
// Cout <countedobject: Count _ <Endl;
Cout <countedobject: getcount () <Endl;
Countedobject * CO2 = new countedobject;
// Cout <countedobject: Count _ <Endl;
Cout <countedobject: getcount () <Endl;
Delete CO2;
// Cout <countedobject: Count _ <Endl;
Cout <countedobject: getcount () <Endl;

}

The above program defines a static member variable and a static member function, which can be accessed by Class Name: static member variable or non-static member function.


Ii. static member functions

The static member function does not have an implicit this pointer.
Non-static member functions can access static members.
Static member functions cannot access non-static members.


C ++ code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
# 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 members.
Teststaticfun ();
}
Static void teststaticfun ()
{
Cout <"teststaticfun..." <Endl;
// Testfun (); error, static member function cannot call non-static member function
// Cout <"Y =" <Y _ <Endl; error. The static member function cannot access non-static members.
}
Static int X _; // description of the reference of static members
Int Y _;
};

Int test: X _ = 100; // definition of static members

Int main (void)
{
Cout <sizeof (TEST) <Endl;

Return 0;
}

Iii. Calculation of class/object size

Class size calculation follows the previously learned structure alignment principle (refer to here)
The class size is irrelevant to the member function (the empty class size is 1 byte)
The class size is irrelevant to the static data member.
The effect of virtual functions on the class size (refer to here)
Influence of virtual inheritance on the class size (refer to here)

Refer:

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

Related Article

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.