[C ++ default function] The six class member functions generated by the empty class by default. The default Function
1. default constructor.
2. Default copy constructor.
3. Default destructor.
4. Default Value assignment operator.
5. default access operator.
6. The default access operator const.
<span style="font-size:18px;">
Class A {public: A () {}// default const A &) {}// copy constructor ~ A () {}// destructor A & operator = (const A &) {}// value assignment operator A * operator &() {}// accessors const A * operator & () const {}// accessors const };
</span>
In C ++, which default member functions does the compiler provide for empty classes? What functions are there?
Empty class. The Compiler does not generate any member functions during declaration.
For empty classes, the compiler does not generate any member functions, but only generates one-byte placeholder.
Sometimes it may be thought that the compiler will generate default constructor for the empty class. In fact, it will not. The compiler will only generate six member functions as needed: A default constructor, a copy constructor, A destructor, a value assignment operator, a pair of access operators, and a this pointer.
Code:
<span style="font-size:18px;">
#include <iostream> using namespace std; class Empty_one { }; class Empty_two {}; class Empty_three { virtual void fun() = 0; }; class Empty_four : public Empty_two, public Empty_three { }; int main() { cout<<"sizeof(Empty_one):"<<sizeof(Empty_one)<<endl; cout<<"sizeof(Empty_two):"<<sizeof(Empty_two)<<endl; cout<<"sizeof(Empty_three):"<<sizeof(Empty_three)<<endl; cout<<"sizeof(Empty_four):"<<sizeof(Empty_four)<<endl; return 0; }
</span>
Result:
Analysis:
Empty_one and Empty_two classes are empty classes, but empty classes can also be instantiated. Each instance has a unique address in the memory, the compiler usually implicitly adds a byte to an empty class, so that the empty class gets a unique address in the memory after instantiation, so the size of sizeof (Empty_one) and sizeof (Empty_two) is 1.
Because Empty_three class has a pure virtual function, there is a pointer to the virtual function (vptr). The 32-bit system allocates four bytes to the pointer, so sizeof (Empty_three) is 4.
The class Empty_four inherits from Empty_two and Empty_three. the compiler removes the placeholder Empty_two and retains a virtual function table. Therefore, the size is 4.
2. Empty class. Six member functions are generated during definition.
When an empty class Empty_one defines an object, Empty_one pt; sizeof (pt) is still 1, but the compiler will generate six member functions: A default constructor, a copy constructor, A destructor, a value assignment operator, and two access operators.
[Html] view plaincopy
- Class Empty
- {};
Equivalent to: [html] view plaincopy
- Class Empty
- {
- Public:
- Empty (); // default constructor
- Empty (const Empty & rhs); // copy the constructor
- ~ Empty (); // destructor
- Empty & operator = (const Empty & rhs); // value assignment operator
- Empty * operator & (); // address fetch operator
- Const Empty * operator & () const; // address fetch operator (const Version)
- };
Call status during use: [html] view plaincopy
- Empty * e = new Empty (); // default constructor
- Delete e; // destructor
- Empty e1; // default constructor
- Empty e2 (e1); // copy the constructor
- E2 = e1; // value assignment operator
- Empty * pe1 = & e1; // address fetch operator (non-const)
- Const Empty * pe2 = & e2; // address fetch operator (const)
The C ++ compiler implements these functions:
[Html] view plaincopy
- Inline Empty: Empty () // default constructor
- {
- }
- Inline Empty ::~ Empty () // destructor
- {
- }
- Inline Empty * Empty: operator & () // address fetch operator (non-const)
- {
- Return this;
- }
- Inline const Empty * Empty: operator & () const // address retrieval operator (const)
- {
- Return this;
- }
- Inline Empty: Empty (const Empty & rhs) // copy the constructor
- {
- // Copy and construct non-static data members of the class one by one in the unit of "members"
- // A fixed object copy structure is a "one-by-one" copy from the source object to the target object.
- }
- Inline Empty & Empty: operator = (const Empty & rhs) // value assignment operator
- {
- // Assign values to non-static data members of a class in the unit of "members"
- // A fixed type of object Value assignment is a "one-by-one" value assignment from the source object to the target object.
- }
For example, m is a non-static member variable of type T in Class C. If C does not declare the copy constructor (Value assignment operator ), m will be copied to the constructor (Value assignment operator) through T. This rule is applied to the data member of m recursively until a copy constructor (Value assignment operator) is found) or a fixed type (for example, int, double, pointer, etc.
Iii. Summary The above running results depend on the compiler and 64-bit and 32-bit systems.