我知道new int()
和new int(10)
之間的區別。在第一種情況下分配0,在第二種情況下分配給新創建的int。但是什麼是new int {}
。我們使用{}進行數組初始化,如new a[]{4,5,6}
。但是對於單變量初始化時使用大括號是什麼意思?C++中新int()和新int {}之間的區別
/* Combined usage and initialized to 0*/
int *ptr2 = new int();
cout<<"*ptr2 = "<<*ptr2<<endl;
/* Allocated memory can be initialized to specific value */
int*ptr3 = new int(5);
cout<<"*ptr3 = "<<*ptr3<<endl;
int* ptr5 = new int{500};
cout<<"*ptr5 = "<<*ptr5<<endl;
的可能的複製[具有或不具有初始化差花括號在C++ 11](https://stackoverflow.com/questions/21150067/initialization-difference-with-or-without -curly-braces-in-c11) – Tas
或者https://stackoverflow.com/questions/18222926/why-is-list-initialization-using-curly-braces-better-than-the-alternatives – Tas