[C ++ default function] The six class member functions generated by the empty class by default. The default Function

Source: Internet
Author: User

[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
  1. Class Empty
  2. {};
Equivalent to: [html] view plaincopy
  1. Class Empty
  2. {
  3. Public:
  4. Empty (); // default constructor
  5. Empty (const Empty & rhs); // copy the constructor
  6. ~ Empty (); // destructor
  7. Empty & operator = (const Empty & rhs); // value assignment operator
  8. Empty * operator & (); // address fetch operator
  9. Const Empty * operator & () const; // address fetch operator (const Version)
  10. };
Call status during use: [html] view plaincopy
  1. Empty * e = new Empty (); // default constructor
  2. Delete e; // destructor
  3. Empty e1; // default constructor
  4. Empty e2 (e1); // copy the constructor
  5. E2 = e1; // value assignment operator
  6. Empty * pe1 = & e1; // address fetch operator (non-const)
  7. Const Empty * pe2 = & e2; // address fetch operator (const)

The C ++ compiler implements these functions:

[Html] view plaincopy
  1. Inline Empty: Empty () // default constructor
  2. {
  3. }
  4. Inline Empty ::~ Empty () // destructor
  5. {
  6. }
  7. Inline Empty * Empty: operator & () // address fetch operator (non-const)
  8. {
  9. Return this;
  10. }
  11. Inline const Empty * Empty: operator & () const // address retrieval operator (const)
  12. {
  13. Return this;
  14. }
  15. Inline Empty: Empty (const Empty & rhs) // copy the constructor
  16. {
  17. // Copy and construct non-static data members of the class one by one in the unit of "members"
  18. // A fixed object copy structure is a "one-by-one" copy from the source object to the target object.
  19. }
  20. Inline Empty & Empty: operator = (const Empty & rhs) // value assignment operator
  21. {
  22. // Assign values to non-static data members of a class in the unit of "members"
  23. // A fixed type of object Value assignment is a "one-by-one" value assignment from the source object to the target object.
  24. }
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.

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.