The default function of a C ++ class

  In C ++, a class eight default functions:

  1. The default constructor;
  2. The default copy constructor;
  3. The default destructor;
  4. The default assignment operator overloading function;
  5. The default address-overloaded operator function;
  6. Overload operator default address-const function;
  7. Mobile default constructor (C ++ 11);
  8. The default assignment operator overloading moving function (C ++ 11).

    A class declaration is just empty, it does not do anything, your compiler will automatically generate a default constructor, a default copy constructor, a default assignment operator overloading default function and a destructor. These functions only when first called, the compiler will be created, of course, several implementations generate default function is to do nothing. All of these functions are inline and the public.

  We do not want the object to be displayed configuration (single mode) or assignment, the corresponding function can be declared as private, or write a base class, open part of the default function, subclass to inherit it. C ++ 11 new identifier default and delete, control whether to use these default functions.

  default: The default function will be identified using the default behavior of the class, such as: A () = default;

  delete: the identified default function will be disabled, such as: A () = delete;

    override: identification function is required or override base class virtual function;

    final: the identified function is the base class virtual function prohibited from been overwritten;

. 1  class A
 2  {
 . 3  public :
 . 4  
. 5      // default constructor; 
. 6      A ();
 . 7  
. 8      // default copy constructor 
. 9      A ( const A & );
 10  
. 11      // Default destructor 
12 is      ~ A ();
 13 is  
14      // default assignment operator overloading function 
15      A & operator = ( const A & );
 16  
. 17      // default overloaded address-operator function 
18 is      A * operator & ();
. 19  
20 is      // default address-reload function const operator 
21 is      const A * operator & () const ;
 22 is  
23 is      // default constructor moved 
24      A (&& A );
 25  
26 is      // default assignment operator overloaded mobile 
27      A & operator = ( const A && );
 28  
29 };

  The following functions are discussed in detail in respect of each study, there are imperfect, comments are welcome correction, your criticism is an inexhaustible power forward next!

A constructor (the Constructor)

1. The effect is the object constructor initializes a new objects on the heap or define a temporary object on the stack, will automatically call the constructor of the object. There initialization list and the constructor body assigned in two ways, initialization list when the object is initialized more efficient (each member can only appear once in the initialization list), a decrease assignment, recommend this method; the following member variable must be initialized initializing the list: constant member variable , a reference type member variables , no member variables default constructor (if there is a class object constructor parameter list, the object's class and there are no default constructor parameters, if it does not use initialization list, the parameters will be called with no arguments or full default constructor, and that the class and no);

2. Functions that are the same as the class name, can overload, not virtual, not have a return value, nor even the void;

3. If you do not explicitly defined, the compiler will automatically generate a default constructor, the default constructor will not do anything;

4. No argument constructor and constructor with default values ​​(default whole) is considered to have a default constructor, and the default constructor only one;

5 this pointer function can be used in vivo, but not for the initialization list. Because the constructor simply initializes the object, before initiating this object already exists, so you can have this, the function body is an assignment, initialization list is for each member variable in the class is initialized, the position of the object initialization incomplete and therefore can not be used this is used to initialize the list;

6. For single-parameter constructor appears to note, C ++ will default conversion parameters corresponding to the type of class type, sometimes the implicit conversion is we do not want, you need to use the explicit keyword to limit this conversion ;

7. construction order: Constructor virtual base class (if there are multiple virtual base class, in the order they are inherited configuration, rather than their order in the initialization list of members);

             Constructor function of a non-virtual base class (if there are multiple non-virtual base class, in the order they are inherited configuration, rather than their order in the initialization list of members);
             member object constructor (if there are multiple members of the class objects in the order they were declared call, rather than their order in the initialization list of members);
             this class constructor. The construction process is recursive.

Second, the copy constructor (Copy Constructor)

1. The copy constructor is overloaded constructor actually has all the characteristics of a general constructor, create a new object with such existing objects, usually copy the value of the data members of the object already exists in the function will to a newly created object in. When using a known object class to initialize another object of this class it will automatically call the copy constructor object;

2. the same function name and category name, the first parameter is a reference to an object is the same, and no other parameters or other parameters have default values, the return value is a reference to the class object can be achieved by continuously constructed return reference value , i.e., similar to A (B (C)) Thus;

3. If not explicitly defined, the compiler will automatically generate a default copy constructor, a default copy constructor will sequentially copy the data members of the class to complete the initialization;

4. shallow vs. deep copy: The default copy constructor will only execute the compiler to create a "shallow copy", that is, through the assignment is completed, if the data members of the class members have a pointer, just copy the address, will make the new copies of the same object with the object that the pointer points to the address of the members, when the pointer delete duplicate delete and will result in an error, if the pointer is out of the new members are "deep copy."

Third, the destructor (Destructor.)

1. destructor role is to do some cleanup, delete an object or objects when the end of the life cycle, will automatically call the destructor of the object;

2. the function name with characters ~ no parameters in the class name (parameter type may have a void), there is no return value, the function may be a virtual (destructor to subclass object through a pointer when the base class) can not be overloaded , so that only a destructor;

3. If not explicitly defined, the compiler will automatically generate a default destructor, default destructor function will do nothing;

Instead sequence and constructors: 4. destructor sequence. The destruction process is recursive.

Fourth, the overloaded assignment operator function (Copy Assignment operator)

1. It is already two objects, one process to another assignment. When the assignment between two objects, will automatically call the overloaded assignment operator function, it differs from the copy constructor is a copy constructor object to the new object has the initial value generating process;

2. assignment operator overloads parameter const and & is not mandatory, the return value is a reference to the class object can be achieved continuous assignment by returning the reference value, i.e. similar to a = b = c Thus, the return type is not mandatory, it may be return void, it can not use continuous assignment;

3. Assignment Operator Overloading function can only be defined as a class member functions, it can not be static member function, nor is a friend, assignment operator overloaded functions can not be inherited, to avoid self-assignment;

4. If not explicitly defined, the compiler will automatically generate a default assignment operator overloaded function, default assignment operator overloads to realize a shallow copy data is assigned one by one member, will cause the pointer suspension problem.

Fifth, the address-Overload operator (const) function

1. Overload address-operator takes no arguments;

2. If not explicitly defined, the compiler will automatically generate a default reload address-operator function, the internal function directly return this, generally use the default.

Six, moving and heavy mobile constructor function assignment operator

1.C ++ 11 move new semantics: control of all resources of the source object to the target object, the object may be moved to a new original object for initialization after a B, a case will be destructed;

Different parameters and copy constructors 2. Move constructor parameter copy constructor is a left reference value, but the initial value is a moving constructor rvalue references;

3. The temporary object is about to die, and inside it is a resource that needs to be recycled, this time we can use the mobile structure. Mobile structure can reduce unnecessary duplication, to bring performance improvement.

Seven, discussion

1. Why constructor can not have a return value?

  (1) .C ++ language has no return value constructor;

  . (2) as constructor without using the right values, the return value is not used;

  (3) Even if the return value from the basic semantic point of view, it should return the object is constructed, so there is no need to bother to specify a return type;

  (4) If the return value, the code discussed below

class A
{
public:
A():m_iTest(0) { }
A(int i):m_iTest(i) { }
private:
int m_iTest;
};

  In accordance with C ++, A a = A (); temporary object is created with a default constructor, with the temporary object and initializes a, at this time, the value should be 0 a.m_iTest. Now if A :: A () returns a value, and returns a 1 (indicating successful configuration), the C ++ will be used to initialize 1 a, i.e. there argument constructor call A :: A (int i), obtained in a. m_iTest will be 1. Thus, semantic ambiguity created, making an already very complex C ++ syntax, further chaos.

  Call the constructor of the reason why no return value, because of the special nature of the decision of the constructor. Of course, the above discussion is based on C ++ language provides that if the provisions of the constructor can have a return value, above usage may not be the same. Is the chicken or the egg, this is a wonderful question. In short, now C ++ grammar system is that, if the design constructors can have a return value, it may be more difficult to achieve throughout the C ++ language.

2. Object creation and destruction process like?

  Object constructor (new) process:

  (1) by the operator new application memory;
  (2) using the constructor call placement new (simple type ignore this step);.
  (3) returns a memory pointer.

  new and malloc comparison:

  (1) .new calls new_handler handler fails, malloc not, return NULL on failure;
  (2) .new can automatically call placement new object's constructor, malloc not;
  (. 3) out of something .new is a band type, malloc is void *, needed to cast;
  (. 4) is a C ++ operator .new, malloc is a standard C library function.

  The new three forms: new operator, operator new, placement new

  (1) .new operator: the above mentioned new operator is new, a total of three steps (for memory, call the constructor returns a memory pointer), for step by the application memory operator new (new operator) to complete, for what constructor calls, it can be determined by placement new;

  (2) .operator new: the same as an ordinary operator can be overloaded, operator new will to apply memory, the application fails when calls new_handler treatment, which is a circular process, if new_handler not throw, will always cycle application memory, until it succeeds;

  (3) .placement new: for positioning constructor specifies the object constructor function of the type used at the specified memory address.

  Objects destroy (delete) process:

  (1) calling the destructor (simple type ignore this step);

  (2) Release memory.

  delete and relatively free

  (1) .delete automatically call the destructor object, free not;
  (2) is a C ++ operator .Delete, free C is a standard library function.

3. Why copy constructor parameter must use class type object reference passed?

 If it passes the reference position has been calling the copy constructor, which is the recursive reference will be, to cause a stack overflow.

4. Why is the assignment operator overloaded function to avoid self-assignment?

 (1) Improve efficiency. Self-assignment meaningless, if self assignment, you can immediately return * this;

   (2) if not avoided, when the data members of the class if it contains a pointer, from the assignment will be a memory leak.

 

Guess you like

Origin www.cnblogs.com/yuwanxian/p/10924835.html