0

Help me with C++ constructors please. Thank you

I wonder what's the reason for my output here. The first line should be all 0s but it's not. The second line is not much meaningful. Actually what does default constructors do? https://code.sololearn.com/c7y54PeptO1d/?ref=app

2nd Jul 2021, 10:45 AM
Rishi
Rishi - avatar
2 Réponses
+ 1
According to language rules, the Date constructor qualifies as a trivial default constructor, which actually performs no action. See: https://en.cppreference.com/w/cpp/language/default_constructor Since you don't provide an initializer for "today", default-initialization is performed, which just selects and calls the trivial default constructor. As a result, the member variables end up uninitialized. See: https://en.cppreference.com/w/cpp/language/default_initialization If you want to zero-initialize a Date object, use value-initialization instead, i.e. Date today{}; See: https://en.cppreference.com/w/cpp/language/value_initialization
2nd Jul 2021, 11:32 AM
Shadow
Shadow - avatar
0
Shadow thank you I got it!
3rd Jul 2021, 2:31 AM
Rishi
Rishi - avatar