How C + + makes a function that returns an object do not call a copy constructor

Source: Internet
Author: User
Tags constructor diff

we know that the copy constructor has two "silent" ways to be called

1. Want the function to pass in the value parameter

2. function return value type

Today we discuss the case of the function return value type.

Get the conclusion that

1. When the object has a copy constructor (the system generates for us, or we write the copy constructor ourselves) can be called implicitly, the function returns with a copy constructor.

2. When the copy constructor declaration of an object becomes explicit (cannot be called implicitly), the function returns with the move constructor.

First look at the starting code.

 #include <iostream> #include <memory> using namespace std;

	Class thing{Public:int Member_; Thing (): Member_ (0) {//default constructor} Thing (thing& Other): Member_ (Other.member_) {//copy constructor cout << "Copy" << ;
	Endl
	Thing (thing&& Other): Member_ (other.member_) {//move constructor cout << "Move" << Endl;

}
};
	Thing Getthingbyreturnvalue () {Thing Thing;
	Thing.member_ = 1;
	cout << "in method T" << &thing.member_ << Endl;
	Return thing;//will call to the copy constructor} unique_ptr<int> getbysmartptr () {unique_ptr<int> p (new int); cout << "in" t << *p << endl;//Note unique_ptr has overloaded the * operator, the actual output is the memory address of the bare pointer return p;//will call UNIQUE_PTR mov
	E constructor} void Main () {Thing th = getthingbyreturnvalue ();

	cout << "Out method T" << &th.member_ << Endl;
	Auto P = getbysmartptr (); cout << "Out method T" << *p << endl;//Note unique_ptr has overloaded the * operator, the actual output is the memory of the bare pointerAddress}//In methods 003cf798 copy out method 003cf88c//Visible Getthingbyreturnvalue call is thing copy constructor in method -842150451 out method-842150451//visible getbysmartptr calls the move constructor for Unique_ptr Press any key to continue ... */

Why does the class we write call copy, and unique_ptr calls move?? Puzzled by the solution. When I sleep, I think of it. Explicit the use of decorated constructors. Try adding explicit to the copy constructor.

Explicit Thing (thing& Other): Member_ (Other.member_) {//copy constructor
	cout << "copy" << Endl;
}

/* In method 003efc18 move out method 003efd0c in method       -842150451      -842150451
Please press any key to continue
... */

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.