Initialization inside constructor body v/s initializer list? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Initialization inside constructor body v/s initializer list?

What is the difference between initializing class data members inside construtor body v/s initializing with constructor initializer list?

2nd Apr 2020, 4:07 AM
Amit chavare
Amit chavare - avatar
4 Answers
+ 1
One of MANY reasons is that when you have a const member. You cannot initialize it inside the constructor, but initializer list. https://code.sololearn.com/cG3PzTSDo6k1/?ref=app Other reasons: https://www.geeksforgeeks.org/when-do-we-use-initializer-list-in-c/
2nd Apr 2020, 8:39 AM
你知道規則,我也是
你知道規則,我也是 - avatar
0
According to the lesson, constructor initializer allows value initialization for const data members. https://www.sololearn.com/learn/CPlusPlus/1896/
2nd Apr 2020, 5:09 AM
Ipang
0
CarrieForle what if my class is have object of another class as it member. Then which would be more preferable to intialize it?
2nd Apr 2020, 8:42 AM
Amit chavare
Amit chavare - avatar
0
Amit chavare You should initialize your members in the initializer list as you can, no matter what type they are.
2nd Apr 2020, 8:52 AM
你知道規則,我也是
你知道規則,我也是 - avatar