C ++ learning-control the replication of class objects (5)

Source: Internet
Author: User

I have learned how to use constructor and the features of initialization list. In this chapter, I will also learn about constructor. The type can also control copying, assigning, or revoking objects of this type. At this time, the class controls these behaviors through special member functions: copying constructors, value assignment operators, and destructor. These are all part of the replication control of class objects.

 


I. Application of the content in this article

1. Example of copying constructor:
String str1 ("hello ");
String str2 (str1 );
2. Example of the value assignment operator:
String str1 ("hello ");
String str2 = str1;

II. Where are the replication constructor applied?
What kind of function is the copy constructor?
There is only one parameter, and this parameter is a reference to this type of object (commonly used for const modification ).
The copy constructor is used:
1. display or implicitly Initialize an object of the same type.
Eg:
String str1 ("hello ");
String str2 (str1 );
2. As the real parameter of the Function
Eg:
Void Show (string str );
...
String str1 ("hello ");
Show (str1); // a copy operation exists. The str1 object is implicitly copied to the temporary object of the Show function.

 


3. As the return value of the Function

Eg:
String getString () const
{
String str ("hello ");
...
Return str;
}
...
String str1 = getString (); // copy the string object returned by the getString function to the str1 object.

 


4. initialize elements of the sequential container

Eg:
Vector <string> vec (5); // declares a vector, which contains five string objects;
// The Compiler first uses the string default constructor to create a temporary object to initialize vec, and then uses the copy constructor to copy the temporary value to each element of vec.

 


5. initialize array elements based on the element initialization list

It should be emphasized that the initialization list
A. Each element calls the default constructor once.
String str [10]; // The default constructor is called 10 times.
B. Each element calls a copy constructor.
String str1, str2, str3, str4;
String str [] = {str1, str2, str3, str4 };

3. copy constructor and value assignment operator for merging
If you do not display a defined copy constructor or value assignment operator, the compiler will synthesize a copy constructor by default. The copying constructor is different from the merging constructor, or the assignment operator is different from the merging assignment operator. By default, the behavior of the function synthesized by the compiler is "initialization by members ", initialize the new object as a copy of the original object. One-by-one initialization does not include static members, but is only responsible for initializing non-static members.

4. Test the tool

Write a simple example to show how to control the replication of class objects.


[Cpp] # include <iostream>
 
Using namespace std;
 
Class CObj
{
Public:
CObj ()
{
Cout <"default constructor" <endl;
}
CObj (const CObj & obj)
{
Cout <"copy constructor" <endl;
}
CObj & operator = (const CObj & obj)
{
If (& obj! = This)
{
Cout <"value assignment operator" <endl;
}
Return * this;
}
};
 
Int main ()
{
CObj obj1;
Cout <endl;
 
CObj obj2 (obj1 );
Cout <endl;
 
CObj obj3;
Obj3 = obj2;
Return 0;
}
# Include <iostream>

Using namespace std;

Class CObj
{
Public:
CObj ()
{
Cout <"default constructor" <endl;
}
CObj (const CObj & obj)
{
Cout <"copy constructor" <endl;
}
CObj & operator = (const CObj & obj)
{
If (& obj! = This)
{
Cout <"www.2cto.com value assignment operator" <endl;
}
Return * this;
}
};

Int main ()
{
CObj obj1;
Cout <endl;

CObj obj2 (obj1 );
Cout <endl;

CObj obj3;
Obj3 = obj2;
Return 0;
}
Execution result:

 

[Plain] default constructor
 
Copy constructor
 
Default constructor
Value assignment operator
Default constructor

Copy constructor

Default constructor
Value assignment operator
Iv. Prohibit Replication
1. Some Classes need to completely prohibit replication. What should I do?
You only need to declare the replication constructor as a private member function.
Explanation: if the definition is not declared, the compiler will synthesize a copy constructor by default.
Example:

 

[Cpp] # include <iostream>
 
Using namespace std;
 
Class CObj
{
Public:
CObj ()
{
Cout <"default constructor" <endl;
}
Private:
CObj (const CObj & obj)
{
Cout <"copy constructor" <endl;
}
};
 
Int main ()
{
CObj obj1;
CObj obj2 (obj1 );
 
Return 0;
}
# Include <iostream>

Using namespace std;

Class CObj
{
Public:
CObj ()
{
Cout <"default constructor" <endl;
}
Private:
CObj (const CObj & obj)
{
Cout <"copy constructor" <endl;
}
};

Int main ()
{
CObj obj1;
CObj obj2 (obj1 );

Return 0;
}
Compilation error:


Error: 'cobj: CObj (const CObj &) 'is private


This is the effect of prohibiting replication, which is exactly what we want here.

 

 

2. Even copying functions and member functions is forbidden. What should I do?
If you want to disable the copying of a member function or a member function, you can declare a private copy constructor, but not define it.
Explanation: Because you can access private members of a class by using a friend or member function, you can certainly call a private copy constructor. All replication constructor statements are private but not defined, you can avoid calls to friends or member functions.
Example:

 

[Cpp] # include <iostream>
 
Using namespace std;
 
Class CObj
{
Public:
CObj ()
{
Cout <"default constructor" <endl;
}
Friend void fcopy ();
Private:
CObj (const CObj & obj );
};
 
Void fcopy ()
{
CObj obj1;
CObj obj2 (obj1 );
}
 
Int main ()
{
Fcopy ();
 
Return 0;
}
# Include <iostream>

Using namespace std;

Class CObj
{
Public:
CObj ()
{
Cout <"default constructor" <endl;
}
Friend void fcopy ();
Private:
CObj (const CObj & obj );
};

Void fcopy ()
{
CObj obj1;
CObj obj2 (obj1 );
}

Int main ()
{
Fcopy ();

Return 0;
}
Compilation error:
Undefined reference to 'cobj: CObj (CObj const &)'
This is exactly the result needed here. It is OK to prohibit successful replication.

 

Make persistent efforts!

From the gzshun Column
 

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.