3

The following is excerpted from section 13.1.1. from book "C++ Prime", 5th edition:

enter image description here

To verify the above paragraph, especially the statement underlined with red, I wrote the following codes:

#include<iostream>
using namespace std;

class TestClass {
public:
    TestClass() :a(7) {
        cout << "Default constructor";
    }
    TestClass(int aa) : a(aa) {
        cout << "Constructor" << endl;
    }
    TestClass(const TestClass & t): a(t.a) {
        cout << "Copy constructor" << endl;
    }
    TestClass & operator=(const TestClass & rhs) {
        a = rhs.a;
        return *this;
    }
    int a;
};

int main() {
    TestClass t1(1);
    TestClass t2 = t1;
}

Based on my understanding of the description of copy initialization in the book, the code should first create t2 using default initializer, then use operator= function to copy the right-hand operand t1. But when I debug line by line in Visual Studio 2015, the code go straight to the copy constructor TestClass(const TestClass & t). This shows that direct initialization and copy initialization are actually doing the same thing, no difference. So, is my understanding wrong or the book is wrong? If I am wrong, what is the correct understanding of the difference between direct initialization and copy initialization? Could you please give me an example code to show such difference? Thanks a lot.

Edit: someone says my question can be answered in this thread. But that thread is only a (detailed and lengthened) repeat of the text I excerpted. It doesn't answer why in practice (e.g., Visual Studio 2015) it is not true.

Community
  • 1
  • 1
user5280911
  • 723
  • 1
  • 8
  • 21
  • 1
    http://stackoverflow.com/questions/1051379/is-there-a-difference-in-c-between-copy-initialization-and-direct-initialization – cpplearner Jan 28 '17 at 07:22

1 Answers1

1

The book just says "copy", which doesn't just mean copy assignment. Note the word "created", copy initialization means construction, not assignment.

For TestClass t2 = t1;, t2 will be copy constructed from t1 via copy constructor directly, not default construction and then assignment.

If T is a class type and the cv-unqualified version of the type of other is T or a class derived from T, the non-explicit constructors of T are examined and the best match is selected by overload resolution. The constructor is then called to initialize the object.

Yes, copy initialization and direct initialization have the same effect in most cases, but there's a difference between them.

Copy-initialization is less permissive than direct-initialization: explicit constructors are not converting constructors and are not considered for copy-initialization.

e.g.

class TestClass {
public:
    // the copy constructor is declared explicit now
    explicit TestClass(const TestClass & t): a(t.a) {
        cout << "Copy constructor" << endl;
    }
    TestClass(int aa) : a(aa) {
        cout << "Constructor" << endl;
    }
    int a;
};

then

int main() {
    TestClass t0(1);
    TestClass t1(t0);  // fine;  explicit constructor works fine with direct initialization
    TestClass t2 = t0; // error; explicit constructor won't be considered for copy initialization
}
songyuanyao
  • 169,198
  • 16
  • 310
  • 405
  • I think my question has nothing to do with explicit. Also, I am asking about how to understand the description on the difference of two types of initialization in the book. I cannot see any connection between your answer and my question. – user5280911 Jan 28 '17 at 07:48
  • @user5280911 It seems you're misunderstanding that the word "copy" in the book means "assignment", so I explained what "copy initialization" means in the 1st part. The part about "explicit", it's because you're saying direct initialization and copy initialization are doing the same thing. – songyuanyao Jan 28 '17 at 07:53
  • I said they are doing the same thing because they are executing the same code. I'm sorry I cannot catch your explanation with regard to "to copy the right-hand operand into the object being created". – user5280911 Jan 28 '17 at 07:58
  • @user5280911 Yes, they do the same thing in most cases, except for the `explicit` thing. And about the expression of the book, it means the object is copy constructed from the right-hand object, not copy assignment. And what copy intialization will do have been explained in my answer. So could you be specific about what you don't understand? – songyuanyao Jan 28 '17 at 08:03