Research on the strange behavior of C ++ copy constructor

Source: Internet
Author: User

C ++ primer (4 edition) Chinese Version p407 said:"During replication initialization, you first use the specified constructor to create a temporary object, and then use the replication constructor to copy the temporary object to the object being created ."However, my lab results show thatThe replication constructor is not called.

I did an experiment (experiment 1). I first wrote a simple class that provides a default constructor and a constructor that accepts a single parameter, A copy constructor and A = overload function print a message in each of the above functions, prompting that the function is being called.

Next, use this class in the main function and use replication to initialize the class object. As stated in the book, the displayed result should be to first call the constructor of a single parameter to create a temporary object, output a prompt, then call the copy constructor, and output a prompt. But there is only one prompt! It is displayed that only the constructors that call the single-form parameters directly create an object and there is no temporary object. The results are weird!

Since I didn't call the copy constructor, I did another experiment (Experiment 2)Copy the constructor to privateIf no changes are made elsewhere and the same experiment is performed, the compilation fails. The error message is that my main function calls the copy constructor. Is the result very strange?

Problem: Does C ++ create a temporary object during replication initialization? If so, why is the result of experiment 1 displayed? If not, why is the result of experiment 2 displayed?

The class definition code of Experiment 1 is as follows:

class Employee{public:    Employee();    Employee(std::string sname);    // copy constructor    Employee(const Employee &old);    Employee& operator=(const Employee&old);     int getID() const { return id; }    std::string getName() const { return name;} private:    static int newid;    int id;    std::string name;};

The class definition code of Experiment 2 is as follows:

class Employee{public:    Employee();    Employee(std::string sname);    Employee& operator=(const Employee&old);     int getID() const { return id; }    std::string getName() const { return name;} private:    // copy constructor    Employee(const Employee &old);    static int newid;    intid;    std::string name;};

The class implementation code of the experiment is as follows:

int Employee::newid =1; Employee::Employee(): id(newid){    std::cout << "In defaultconstructor, id = " << id << std::endl;    ++newid;} Employee::Employee(std::stringsname) : id(newid), name(sname){    std::cout << "In singleparameter constructor, id = " << id << std::endl;    ++newid;}// copy constructorEmployee::Employee(constEmployee &old) : id(newid), name(old.getName()){    std::cout << "In copy constructor,id = " << id << std::endl;    ++newid;} Employee&Employee::operator=(const Employee &old){    std::cout << "In aissignfunction, id = " << id << std::endl;    name = old.getName();    return *this;}

The main function uses the following code:

int main(){    string name = "adu";    Employee em5 = name;     return 0;}

Result 1:

In single parameterconstructor, id = 1

Experiment 2 results:

Error: 'employee: employee (const employee &) 'is private

The issue was posted on the csdn Forum. You are welcome to discuss it together.

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.