Direct Initialization : whenever the initialization is an argument list in parenthesis.
Copy-initialization only takes into account implicit constructors and implicit user-defined conversion functions, whereas direct-initialization takes into account all implicit constructors and implicit user-defined conversion functions.
for example:
Foo foo(1,2);
int i(10);
Case 1: Explicit – Copy initialization will not work with explicit constructors
struct Foo {
explicit Foo(int) {}
};
Foo foo1 = 1; // Error - as copy initialization will not work
Foo foo2(10); //OK
Case 2: both explicit and normal constructor
struct Foo {
explicit Foo(int) {}
Foo(double) = {}
};
Foo foo1 = 1; // OK - calls Foo(double)
Foo foo2(10); //OK - calls explicit Foo(int)
Case 3: problem with direct initialization – passing object
struct Foo{ }
struct Bar{
Bar(Foo) {}
}
int main()
{
Bar bar(Foo()); // It will be treated as function declaration
}