Search

9.12 — Copy initialization

Consider the following line of code:

This statement uses copy initialization to initialize newly created integer variable x to the value of 5.

However, classes are a little more complicated, since they use constructors for initialization. This lesson will examine topics related to copy initialization for classes.

Copy initialization for classes

Given our Fraction class:

Consider the following:

If you were to compile and run this, you’d see that it produces the expected output:

6/1

This form of copy initialization is evaluated the same way as the following:

And as you learned in the previous lesson, this can potentially make calls to both Fraction(int, int) and the Fraction copy constructor (which may be elided for performance reasons). However, because eliding isn’t guaranteed, it’s better to avoid copy initialization for classes, and use direct or uniform initialization instead.

Rule: Avoid using copy initialization, and use uniform initialization instead.

Other places copy initialization is used

There are a few other places copy initialization is used, but two of them are worth mentioning explicitly. When you pass or return a class by value, that process uses copy initialization.

Consider:

In the above program, function makeNegative takes a Fraction by value and also returns a Fraction by value. When we run this program, we get:

Copy constructor called
Copy constructor called
-5/3

The first copy constructor call happens when fiveThirds passed as an argument into makeNegative() parameter f. The second call happens when the return value from makeNegative() is passed back to main().

In the above case, both the argument passed by value and the return value can not be elided. However, in other cases, if the argument or return value meet specific criteria, the compiler may opt to elide the copy constructor. For example:

In this case, the compiler will probably elide the copy constructor, even though variable s is returned by value.

9.13 -- Converting constructors, explicit, and delete
Index
9.11 -- The copy constructor

10 comments to 9.12 — Copy initialization

  • anand

    This form of copy initialization is evaluated the same way as the following:

    ;
    is it not supposed to be:

    ;

  • Nyap

    This is whats really getting me confused

    However, because eliding isn’t guaranteed, it’s better to avoid copy initialization for classes, and use direct or uniform initialization instead.

    But isn’t that the same for all forms of initialization? that eliding is not guaranteed? or is the compiler just less likely to elide if its in copy init form?

    • Alex

      There’s no need to elide direct or uniform initialization because they are already as efficient as possible (there’s no extraneous objects created).

  • Andrew Terry

    If you were to compile and run this, you’d see that it produces the expected output:

    6

    No it is 6/1

  • Nazime

    In the first exemple in the main  you forgot the " () " juste after the main
    Consider the following:

    Whene i copied that i get an error and i didn’t notice that at the first place x)
    i Like this website *_*

  • Vijay

    Hi Alex, why in this program compiler elide the copy constructor?

Leave a Comment

Put C++ code inside [code][/code] tags to use the syntax highlighter