if you don't write default construtor for a c++ class (.h and .cpp files) could any errors/issues arise?
Team Fuki
I'm late to this party but what can I do?
(I'm restricting my response to construction errors only.)
It depends on the class.
A default constructor is generated when you don't specify one and don't provide a custom class constructor. A simple class
struct A { char id; };
has no constructors of any kind so a default constructor is generated. The generated default ctor will use the default ctor for each class member variable; in this case the default ctor for a char. For the built-in types, like char, the default ctor simply initializes the value to zero.
If you don't provide a class default ctor and a class member variable itself doesn't have a default ctor then the compiler will complain when it tries to generate the default ctor for your class.
struct A { int id; A(int val) // custom ctor : id(val) { } }; struct B // generated default ctor fails { A a; // no default ctor };
The compiler doesn't know how to initialize the class member object 'a'.
If you provide a custom ctor for your class then the default ctor is not generated for you. Your custom ctor is responsible for the proper initialization of the class member variables. The compiler will complain if you fail to do so.
struct A { int id; A(int val) // custom ctor : id(val) { } }; struct B { A a; B(int val) // fail to initialize 'a' { } };
You can provide your own default ctor but you are still responsible for the class member initialization
struct A { int id; A(int val) // custom ctor : id(val) { } }; struct B { A a; B() // default ctor : a(10) // member ctor { } B(int val) : a(val) { } };
There are other errors that can result from a missing default ctor, like usage in a container class or temporary objects in function or assignment, etc., which rely on a default ctor for an object.