C ++ base 4: constructor, destructor, copy destructors, static member functions

[Constructors]
1.1 Constructor: A special function the same as the type name , no return value type, to ensure that when you create an object, automatically call once, a class can have multiple constructors
role: initialize the object
, if a class does not provide a constructor function, the system automatically provides a no-argument constructor, but once a constructor, the configuration of the system without parameters automatically disappear.

1.2 creation of an object
based on object size, memory allocation, if the class member variable is a primitive type, then do nothing; if it is a member of a class type, then to construct it, call the constructor of this class

1.3 initialization parameter list
when the type has const member, or a reference member type, requires the assignment before the constructor call, after the construction of the parameter list functions, to achieve position before the body is initialization parameter list
NOTE: constructor directly assignment:
class a {
    const int A;
    int B;
    int & C;
public:
    A (int OC): A (123), B (99), C (* new new int (OC)) {
        // A = 100;
        // B = 200 is;
        COUT < <A << "," << << B "," C << << endl;
    }
};


 

}

 

 

[Destructor]
2.1 destructor: a special function, and the class names are the same, but there is a ~ before the function name can not have any parameters and returns no value type, the automatic call before an object is destroyed once

what 2.2 when need to customize destructor
when object is destroyed, it is necessary to release resources, there is generally a dynamic memory allocation

2.3 allocate memory in the constructor or initialization parameter list as a member variable pointer class relieve dynamic in destructor memory allocation

call sequence 2.4 constructor and destructor: the first configuration destructor

 

[Copy constructor]
copy constructor: is a special constructor is used an existing one, and to create another of the same type of the object
class name (const class name & new parameter name)
class A {
public:
A () ... {
}
a (a & a const) {
}
};

3.2 copy constructor call time
using the same type of objects, to create another object
a a;
a B = a;


The object is assigned to the function parameter
showa (A a)

to an object as the return value of the function
A Geta (A A) {
return A;
}

3.3 when required custom copy constructor
want to customize the copying process, need to object has its own independent space, the default copy constructor is the original data byte by byte object copies

 

Static member function [] and [] static member variables
4.1 static member function that does not require the object member function can be called directly
4.2 Static Control by the class name and the scope of authority of the
4.3 static member functions can only access static members can not directly access nonstatic member
4.4 static member variables must be initialized outside the class, if the static member is a primitive type, then initialized to 0, if a custom type, is automatically invoked constructor with no arguments
class A {
static int A;
};
static member variable type the name of the class :: variable name;

4.5 static member function pointers without this, ordinary member functions, the compiler is responsible for passing a pointer to this, while the static function call without going through the object, it does not pass this pointer

 

Published 69 original articles · won praise 37 · views 180 000 +

Guess you like

Origin blog.csdn.net/xi_gua_gua/article/details/59207642