The term "Default Constructor" is official in both Java and C++ and it seems meaning different thing in the two worlds. May I know if my understanding is correct and what is the proper naming of some related concepts?
Default Constructor in Java means the (No-arg) Constructor generated by Compiler when there is no constructor defined for a class.
Default Constructor in C++ means a constructor that can be called with no argument.
Given the following example
Java:
class NoCtorClass {
// No ctor defined, compiler is generating one --------- J-1
}
class NoArgCtorClass {
public NoArgCtorClass() { ... } -------------------------- J-2
}
C++:
class NoCtorClass {
// implicitly NoCtorClass() constructor is provided -------- C-1
}
class DefaultCtorClass {
public:
// Explicitly telling compiler to give the default one
DefaultCtorClass() = default; ---------------------------- C-2
}
class NoArgCtorClass {
public:
NoArgCtorClass(); ----------------------------------------- C-3
}
NoArgCtorClass::NoArgCtorClass() {....}
class NoArgCtor2Class {
public:
NoArgCtor2Class(int i = 0); -------------------------------- C-4
}
NoArgCtor2Class::NoArgCtor2Class (int i = 0) {....}
in Java, only J-1 is officially called default constructor, while J-2 is not.
In C++, all C-1 to C-4 are officially called default constructor.
Is my understand correct?
If so, some questions in terminology:
What is the proper name in Java for ctor without argument? (i.e. J-1 and J2). I usually call it No-Arg Constructor. (For which corresponds to the concept of default-ctor in C++)
What is the proper name in C++ for ctor generated by compiler? (i.e. C-1 and C-2. With the keyword
default
, it seems should be called default. Then should it be called "default default constructor"? (For which corresponds to the concept of default-ctor in Java)Similar to 2, how should we call the compiler generated Copy-ctor, assignment operator, and etc? "Default-Copy-Constructor"?