How are the new differences between POD and user-defined data types?

Firstly:

int *p = new int; 

The second:

 class A{}; A *pa = new A; 

How does the new one and the compiler determine when to call the constructor? In the first case, the compiler does not generate code to call constructor p, and in the second case, it generates code to call constructor A. What mechanism is used to select?

+4
source share
2 answers

The compiler knows that A is a class because it saw the class declaration, so it uses a synthesized default constructor. He knows that int is int, because the grammar of the language says that it is.

+7
source

In your case, A is also a POD. To find out the correct POD definition, check out this one .

As for your code, the compiler knows that int is a built-in type and has no constructor at all.

Edit:. Your question is rather strange. The compiler knows which type is a module and which is not, he also knows which ones are built-in and not built-in, because it is a compiler that compiles your code :) If the compiler did not know this information, who would it be?

+6
source

Source: https://habr.com/ru/post/1332486/


All Articles