Can you explain this cryptic code?

This was discovered when debugging C ++ code in Embarcadero RAD Studio. It seems to be compiling, but frankly, although it seems obvious what it was supposed to do, I cannot understand what it is actually doing.

TObject *objPtr ( new TObject() ); 

If anyone could offer a reasonable explanation, I would appreciate it.

+6
source share
2 answers

It uses direct initialization syntax to initialize objPtr for the newly assigned Tobject. For most practical purposes, this is equivalent to Tobject *objPtr = new Tobject(); .

+4
source

This creates an object of type TObject on the heap and stores its location in a TObject pointer called objPtr. It should be deleted through the delete objPtr line at some point in order to prevent a memory leak.

+1
source

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


All Articles