What the difference between A a1(5); and A a2 = A(5) ? Both of the works, but I really want know the difference between them, because I used method 2 in one of my project and I suffered from a bug which is fixed after I change to method 1. Thanks in advance!
class A {
public:
int val;
A() : val(0) {}
A(int newVal) : val(newVal) {}
};
int main()
{
A a1(5); // method 1
A a2 = A(5); // method 2
}
A a1(5); // method 1
A a2 = A(5); // method 2
The first one is called direct initialization, the second one is called copy-initialization.
The second one will NOT compile if you make the copy-constructor inaccessible or/and don't define it as:
class A {
public:
int val;
A() : val(0) {}
A(int newVal) : val(newVal) {}
private:
A(A const&); //the second one will not compile
};
Now the second one will not compile. Note that it will not compile in both cases:
private
or protected
).Strictly speaking, reading your question I expected you to provide these two examples
A a1(5); // method 1
A a2 = 5; // method 2
The first one is direct-initialization. The second one is copy-initialization.
The examples you provided in your question actually already illustrate the difference between the two :)
Direct-initialization initializes the target object directly, as a single-step process by finding and using an appropriate constructor. Copy-initialization is conceptually a two-step process: first it constructs a temporary object of type A
by some conversion constructor and then it copies it to the destination object by using a copy-constructor. I.e.
A a2 = 5;
will actually work as
A a2 = A(5);
And that actually exhaustively explains the difference.
The two-step structure of the second variant is, again, conceptual. The compilers are allowed (and will) optimize the second variant so that it will work exactly as the first one. They will eliminate the intermediate temporary object and perform initialzation directly.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With