Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Will default constructor still be created if I explicitly define a constructor with arguments?

Will default constructor still be created if I explicitly define a constructor with arguments but no default constructor?

Thank you!

like image 554
user3027198 Avatar asked Oct 27 '25 10:10

user3027198


1 Answers

No, you have to explicitly provide the default constructor in that case.

Although you can (from C++11 onwards), for a class Foo, use the syntax

Foo() = default;

within the class declaration which reintroduces the compiler-generated default constructor. (Note you need to put this in the public section for exact equivalence).

Alternatively, if you provide default arguments to all the constructor parameters, then it becomes the default constructor.

like image 91
Bathsheba Avatar answered Oct 28 '25 23:10

Bathsheba



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!