What is a replication constructor? Generally, the system has a default replication constructor, which copies all the members of the class in sequence and we will not use it manually, for example, when an object generates a copy, the system performs the copy constructor. What is the purpose of general program development? We can use a custom replication constructor to copy classes in our own way, or prohibit replication ......
# Ifndef student_h # define student_h # include <iostream> using namespace STD; class student {public: Student (); student (INT num, string name ); // reload the constructor student (student & St); // copy the constructor. The format is unique and must be referenced. Void copystudent (student st); // test the effect of copying constructor void print (); Virtual ~ Student (); protected: Private: int number; string name ;};# endif // student_h
# Include ".. /include/student. H "Student: Student () {// ctor number = 0; this-> name =" 0000 ";} Student: Student (INT num, string name) {// ctor number = num; this-> name = Name;} Student: Student (student & St) {name = "copy"; // custom copy mode, copy to copy} void Student: Print () {cout <"number:" <number <Endl; cout <"name:" <name <Endl; cout <Endl;} void Student: copystudent (student st) {number = ST. number; this-> name = ST. Name;} Student ::~ Student () {// dtor}
#include <iostream>#include "include/student.h"using namespace std;int main(){ student st0; student st1(1,"lee"); st0.print(); st1.print(); cout<< "after copy" <<endl; st0.CopyStudent(st1); st0.print(); return 0;}
If there is no custom replication constructor in the entire program, void copystudent (student st); the function will copy the members of an object to another object, this process is passed through the copy St. However, after the replication constructor is defined, the St replica cannot be copied by default. Instead, it is transmitted using the custom replication constructor. In this way, you can disable replication.
This article was written after I learned how to copy constructor, but I still don't know how to use it flexibly in development. I hope you will give me more advice and have the best examples. In addition, I have a deeper understanding of the transfer of the value of the function.