Yes, all three initialization methods you've provided will call the default constructor of the Foo class, assuming that Foo has a default constructor defined. Here's a detailed breakdown of each initialization style:
1. Direct Initialization Without Parentheses
Foo f; // Clear intent to create an object
- Behavior: This is known as default initialization.
- What Happens:
- If
Foo is a class type with a user-declared default constructor, that constructor is called.
- If
Foo is a class type without any constructors, the object is default-initialized, which means its members are not initialized (left with indeterminate values).
- For non-class types, default initialization will leave the variable uninitialized if it's a local variable.
2. Uniform Initialization (C++11 and Later)
Foo f{}; // Modern and unambiguous
- Behavior: This uses list initialization (also known as uniform initialization).
- What Happens:
- Value Initialization: When you use empty braces
{}, it triggers value initialization.
- For class types, value initialization calls the default constructor.
- If
Foo has no user-declared constructors, it ensures that all members are zero-initialized.
3. Copy Initialization When Appropriate
Foo f = Foo(); // Explicit and clear
- Behavior: This is known as copy initialization.
- What Happens:
Foo() creates a temporary object via value initialization, which calls the default constructor.
Foo f = Foo(); then initializes f by copying or moving the temporary object into f.
- Optimization (Copy Elision): Modern compilers often elide the copy or move, resulting in a single call to the default constructor instead of two.
Summary
- Direct Initialization (
Foo f;): Calls the default constructor.
- Uniform Initialization (
Foo f{};): Calls the default constructor via value initialization.
- Copy Initialization (
Foo f = Foo();): Calls the default constructor (potentially optimized to a single call).
Important Considerations:
- Explicit Constructors: If
Foo has an explicit default constructor, direct and uniform initialization will work, but copy initialization might not, depending on how it's defined.
- No Default Constructor: If
Foo doesn't have a default constructor, both direct and uniform initialization will result in a compilation error. Copy initialization would also fail unless there are other constructors that can handle the initialization.
In conclusion, under typical circumstances where Foo has a default constructor, all three initialization methods effectively invoke the default constructor.
Example:
#include <iostream>
struct Foo {
Foo() { std::cout << "Default constructor called\n"; }
Foo(const Foo&) { std::cout << "Copy constructor called\n"; }
};
int main() {
Foo f1; // Default constructor called
Foo f2{}; // Default constructor called
Foo f3 = Foo(); // Default constructor called (copy elided)
return 0;
}
Output:
Default constructor called
Default constructor called
Default constructor called
All three forms of initialization invoke (directly or through value initialization) Foo’s default constructor.