Copy constructor and prohibit copy, that is, the principle of passing function values

Source: Internet
Author: User

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.

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.