Whats the difference between the default constructor and deafult argument constructor? An example will be helpful
Asked
Active
Viewed 172 times
2
-
what is a "default argument constructor" ? – 463035818_is_not_an_ai Oct 06 '20 at 11:35
-
@idclev I think something like the difference between `void foo();` and `void foo(int bar = 1);` – JohnFilleau Oct 06 '20 at 11:35
-
well, let's assume class `A` has two constructor `A()` and `A(int a=0)`. when you declare the variable `A a();` in some scope, what does it mean as you think? Is it `A()` or `A(0)` ? – Joona Yoon Oct 06 '20 at 11:38
-
2@JoonaYoon `A a();` declares a function that returns an `A`. – 463035818_is_not_an_ai Oct 06 '20 at 11:39
-
@idclev463035818 No, it is an instance `a` of type `A`. i told it is declared in *some scope*, not a defining function. – Joona Yoon Oct 06 '20 at 11:44
-
1@JoonaYoon https://stackoverflow.com/questions/14077608/what-is-the-purpose-of-the-most-vexing-parse It declares a function called `a` that returns an `A` – 463035818_is_not_an_ai Oct 06 '20 at 11:46
-
@idclev463035818 okay i got it. but it is not the problem about this question what it is function or something. ambiguity rely on calling which constructor. – Joona Yoon Oct 06 '20 at 11:51
-
@JoonaYoon I know this isnt the problem of the question, thats why I think your comment can be deleted. It is misleading – 463035818_is_not_an_ai Oct 06 '20 at 11:54
-
@idclev463035818 so could you look at line 9 on [this code](https://ideone.com/KlVyib)? declaration of its is function? not an instance by calling a default constructor? i am so confused... – Joona Yoon Oct 06 '20 at 12:04
-
@JoonaYoon look at this error message: https://godbolt.org/z/obPq79 – 463035818_is_not_an_ai Oct 06 '20 at 12:07
-
1@idclev463035818 OMG. great thanks. i used it sometimes but its first time got error msg. but I can not edit above wrong comment by system. sorry about that. i won't delete a comment for good history to me. thx. – Joona Yoon Oct 06 '20 at 12:12
2 Answers
5
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.

463035818_is_not_an_ai
- 109,796
- 11
- 89
- 185
-
@YatharthKumar by having only a single default constructor. Having more than one is uncommon – 463035818_is_not_an_ai Oct 06 '20 at 13:51
-
@YatharthKumar yes, you can keep both, you can call the first when you provide an argument to it, but you cannot call either of them without argument, because they are ambiguous. If you have problem with some code, I can only help you after seeing the code. Maybe you want to open a new question to ask about your actual problem. Please do not change this question to ask for something else, because the current answers answer the question as is – 463035818_is_not_an_ai Oct 06 '20 at 14:27
2
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.)

Bathsheba
- 231,907
- 34
- 361
- 483