Consider the following:
template <class T>
struct myclass
{
using value_type = T;
constexpr myclass() = default;
constexpr myclass(const myclass& other) = default;
constexpr myclass(const myclass&& other) = default;
T value;
};
myclass<int> x;
initialize the integer at 0
?myclass<std::vector<int>> x;
what does the default move constructor do? Does it call the move constructor of the vector?The default constructor in Java initializes the data members of the class to their default values such as 0 for int, 0.0 for double etc. This constructor is implemented by default by the Java compiler if there is no explicit constructor implemented by the user for the class.
Yes, it is possible to call special member functions explicitly by the programmer.
Assuming you're talking about C++ (anyway, this should be similar in most other languages), if you don't call a constructor of the base class explicitly, its default constructor will be called automatically (if one exists; if not, the compiler would fire an error).
What is the default constructor? Java doesn't require a constructor when we create a class. However, it's important to know what happens under the hood when no constructors are explicitly defined. The compiler automatically provides a public no-argument constructor for any class without constructors.
They aren't equivalent to any function bodies. There are small but significant differences between the three cases: = default
, allowing implicit generation, and the nearest equivalent function body.
The following links explain in more detail:
I couldn't find a good link about copy-constructor; however similar considerations as mentioned in the other two links will apply.
myclass<int> x;
does not set value
to 0
.
The defaulted move-constructor (if you made it a non-const reference) moves each movable member (although I think there is a special case where if there is a non-movable base class, weird things happen...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With