Let's say I have a pointer and I want it to be initialized to nullptr. Which option would be better, to create a constructor that sets that member variable to nullptr or just initialize it in the declaration on the header file to nullptr.
Is there any differences at all? Is the latter considered bad practice?
Thanks in advance.
Initializing a member in the class body is completely equivalent to doing it in the member initializer list in the construcor. (But if you provide initializers in both places, member-init-list overrides initializers in class body.)
Initializing in the class body whenever possible is a good practice, because it's less error prone (you'll immediately notice if you forget to initialize a newly added member; see DRY).
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