Reputation: 19
Whats the difference between the default constructor and deafult argument constructor? An example will be helpful
Upvotes: 2
Views: 201
Reputation: 234785
A constructor where all the arguments have defaults is also a default constructor for the class.
struct Foo
{
Foo() = default;
Foo(int = 0){};
};
int main() {
Foo f;
}
will not compile as there are two candidate default constructors so overload resolution will fail. (Note that Foo f(1);
will compile as overload resolution is no longer ambiguous.)
Upvotes: 2
Reputation: 122830
I suppose you mean something like this:
struct foo {
foo(int x = 1) {}
foo() {}
};
A default constructor is one that can be called without arguments (and I suppose that is what you misunderstood). Both constructors above are default constructors. They both can be called without arguments and when you call the constructor via
foo f;
Both are viable and there is no way for the compiler to resolve the ambiguity.
Upvotes: 5