Constructor: A function that is the same as the class name is a constructor, and there is no return value type declaration.
Classification of constructors: parameterless constructors, parametric constructors, assignment (copy) constructors
Test ()//no parameter constructor definition test (INTA, int b)//Parameter constructor definition test (const test &obj)//copy constructor definition
The following three constructors are discussed separately
1. No parameter constructor
Call method: Test T1, T2; That's a good understanding.
2 parametric constructors
Three methods of invocation:
Class test5{Public:test5 (INTA); TEST5 (int A, int b);} void Main () {Test5 T1 ()//c++ compiler calls the argument constructor by default Test5 t2 = (ten);//c++ compiler calls the argument constructor by default the Equals method Test5 t3 = TEST5 (30);//Programmer manual Tuning A constructor is used to generate an object directly called by the constructor constructor method}
3. Copy Constructors
Why copy constructors--solving replication problems for class objects
For normal types of objects, replication between them is simple, for example:
int A=88;int b=a;
And the class object is different from ordinary object, the inner structure of class object is generally more complex, there are various member variables. Let's look at a simple example of a class object copy.
#include <iostream>using namespace Std;class cexample {private:int a;public:cexample (int b) {a=b;} void Show () { cout<<a<<endl;}}; int main () {cexample A (100); Cexample B=a; B.show (); return 0;}
Run the program, screen output 100. As can be seen from the running results of the above code, the system allocates memory for object B and completes the copy process with object A. In the case of a class object, a class object of the same type is done by copying the constructor to complete the entire copy process. The following example illustrates the working process of a copy constructor.
#include <iostream>using namespace Std;class cexample {private:int a;public:cexample (int b) {a=b;} Cexample (const cexample& C) {a=c.a;} void Show () {cout<<a<<endl;}}; int main () {cexample A (100); Cexample B=a; B.show (); return 0;} Cexample (const CEXAMPLE&C)
The
is our custom copy constructor. As can be seen, the copy constructor is a special constructor, the name of the function must be the same as the class name, and its only parameter is a reference variable of this type, which is a const type and immutable. For example, the copy constructor for Class X is in the form of X (x& x).
When you initialize another newly constructed object with a custom class type Object that has already been initialized, the copy constructor is automatically called. In other words, the copy constructor is called when the object of the class needs to be copied. The copy constructor is called when a
object is passed in the function body as a value
An object is returned from the function as a value.
An object needs to be initialized by another object.
If a copy constructor is not explicitly declared in the class, the compiler will automatically generate a default copy constructor that completes a bit copy between objects. A bit copy is also called a shallow copy, which is explained later. The
Custom copy constructor is a good programming style that prevents the compiler from forming a default copy constructor to improve the efficiency of the source code.
Shallow copy and deep copy
In some cases, the in-class member variable needs to dynamically open up heap memory, if a bit copy is implemented, that is, the value in the object is completely copied to another object, such as A=b. At this point, if a member variable pointer in B has already applied for memory, that member variable in a also points to the same piece of memory. There is a problem: when B releases the memory (for example, a destructor), a pointer inside a is a wild pointer, and a run error occurs.
Deep and shallow copies can be understood simply as: If a class has resources, when the object of this class has a replication process, the resource is redistributed, the process is a deep copy, and conversely, no redistribution of resources, is a shallow copy. Here is an example of a deep copy.
#include <iostream>using namespace Std;class ca{public:ca (int b,char* cstr) {a=b; Str=new Char[b]; strcpy (STR,CSTR); } CA (const ca& C) {a=c.a; Str=new Char[a]; Deep Copy if (str!=0) strcpy (STR,C.STR); } void Show () {cout<<str<<endl; } ~ca () {delete str; } Private:int A; char *str;}; int main () {CA A (ten, "hello!"); CA B=a; B.show (); return 0;}
The definition of deep and shallow copies can be easily understood as: If a class has resources (heaps, or other system resources), the process can be called a deep copy when the object of the class is copied, whereas the object has resources, but the replication process does not replicate the resources as a shallow copy.
A shallow copy of a resource will cause the program to run out of error when it frees up resources to generate an ambiguous resource.
Test &c_t is a custom copy constructor, and the name of the copy constructor must be the same as the class name, and the formal parameter of the function is a reference variable of this type and must be a reference.
When you initialize another newly constructed object with a custom class type Object that has already been initialized, the copy constructor is automatically called, and if you don't have a custom copy constructor, the system will provide a default copy constructor to complete the process. The copy core statement of the above code is a copy of the P1=C_T.P1 within the constructor by using Test &c_t, which completes the statement.
The above is the C + + review key summary of three--the contents of the constructor, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!