Why constructor is not called | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Why constructor is not called

Please refer code below : I am not able to observe constructor call... if i remove const from initializer list constructor, normal constructor is getting called... why so ? https://code.sololearn.com/c2NAi7fyK6LM/?ref=app

12th Jun 2022, 1:10 PM
Ketan Lalcheta
Ketan Lalcheta - avatar
1 Answer
+ 1
If you remove 'const', then the 2nd constructor would be expecting a lvalue reference to an initializer_list. The initializer_list in test obj{1}; Is not an lvalue, so the 1st constructor that takes an int would be chosen. This reasoning can be proven by passing another argument in the initializer_list test obj{1, 2}; which would result in a compiler error. Or, by changing the type of parameter in the 2nd constructor to initializer_list<int>&&
12th Jun 2022, 3:18 PM
XXX
XXX - avatar