R
R
rxxxxxx2020-11-11 14:26:12
C++ / C#
rxxxxxx, 2020-11-11 14:26:12

The syntax of the new operator?

I have been learning C++ for about 1.5 months.
Recently I saw in a video of a person how, when using dynamic memory, he uses something like this:
A *ptr = new B;
A and B are classes.
Before that, I used dynamic memory when the data types were the same, but here in the example they are different. And I don't understand how they differ. I know that the data type that appears before the name of the variable is the data type of the variable itself, but what does the data type that is written after the new operator mean?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2020-11-11
@jcmvbkbc

what does the data type that is written after the new operator mean?

The type of the object being created. The answer to this question can be obtained by looking at the standard .
but here they are different

For this to work, A must be, for example, a single-valued public ancestor of B.

O
olkhovichs, 2020-11-11
@olkhovichs

In general, the data type that is written after the operator newcan mean any fundamental data type or user-defined object. In this case , this is the type of the class, which is apparently inherited from the class
A* ptr = new B
BA

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question