new T vs new T() | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

new T vs new T()

Hi If I am not wrong , new T doesnot call constructor for our own defined class. It's like malloc just memory allocation.... To have constructor call , we need to create object using new T();... Such happens when we have defined our own constructor... If constructor is from compiler, there is no difference between new T and new T()... Is above correct ? However, I am not getting any difference for both type of object in both classes with or without our own constructor. Below is code: https://code.sololearn.com/c320BMUYeZxq/?ref=app Conclusion : what is difference between new T and new T()

25th Jun 2020, 10:47 AM
Ketan Lalcheta
Ketan Lalcheta - avatar
4 Answers
25th Jun 2020, 11:13 AM
Dennis
Dennis - avatar
0
No your understanding is not correct. In both cases the default constructor will be called. There is no difference between the 2 constructs
25th Jun 2020, 11:10 AM
Anthony Maina
Anthony Maina - avatar
0
Oh okay I didn't know that
25th Jun 2020, 11:16 AM
Anthony Maina
Anthony Maina - avatar
0
So is it good I mean safe to go with new T() over new T in any version of compiler for any case of class.... Isn't it ? I used to define new T till the date be it any compiler version...
25th Jun 2020, 4:36 PM
Ketan Lalcheta
Ketan Lalcheta - avatar