Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you explain this mysterious code?

Tags:

c++

Found this while debugging C++ code in Embarcadero RAD Studio. It appears to compile, but frankly, while it seems obvious what it's meant to do, I can't figure out what it's actually doing.

TObject *objPtr ( new TObject() );

If anyone could offer a sane explanation, I would be grateful.

like image 880
RedWolf Avatar asked Apr 19 '11 01:04

RedWolf


2 Answers

It's using direct initialization syntax to initialize objPtr to a newly allocated Tobject. For most practical purposes, it's equivalent to Tobject *objPtr = new Tobject();.

like image 61
Jerry Coffin Avatar answered Oct 04 '22 17:10

Jerry Coffin


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

like image 31
Jeff Linahan Avatar answered Oct 04 '22 18:10

Jeff Linahan