Do parentheses after the type name in the new operator affect initialization?
Hi everyone! I started learning C++ and encountered something unclear. When we allocate memory via `new`, I often see two writing variants:
```cpp
int* p1 = new int; // without parentheses
int* p2 = new int(); // with parentheses
```
Or for classes:
```cpp
MyClass* obj1 = new MyClass;
MyClass* obj2 = new MyClass();
```
It seems there's no difference, but I heard somewhere that for built-in types (int, double, etc.) parentheses can affect initialization. I checked with a simple example:
```cpp
#include
int main() {
int* a = new int;
int* b = new int();
std::cout << *a << " " << *b; // Sometimes prints garbage and 0
delete a;
delete b;
return 0;
}
```
Indeed, `*a` shows a strange value, while `*b` is always zero. Why is that? And for classes, is there any difference?
No answers yet.
Log in to leave an answer