Copy construct hidden traps

Source: Internet
Author: User

Copy Construction

Be careful when you see the value assignment symbol, because the "=" syntax can also be used to call the copy constructor.
For example:Widget W3 = W2; // call the copy constructor !!
The copy constructor is a particularly important function because it defines how an object is passed by value (passed by value ).
For example, considerCode:
Bool hasacceptablequality (widget W );
...
Widget awidget;
If (hasacceptablequality (awidget ))
...

The statement for decomposing hasacceptablequality (awidget) is as follows:
Widget W = awidget;
Hasacceptablequality (W)

The W parameter is passed to hasacceptablequality by value. In the preceding call, the widget is copied to the W body. This copy operation is completed by the copy constructor of the widget.

 

Conclusion: The pass-by-value in the function indicates "calling the copy constructor".

Passing user-defined types by value is usually a bad idea. pass-by-reference-to-const is usually a good choice.

Reprinted please note,ArticleFrom:Http://blog.csdn.net/windows_nt

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.