简体   繁体   English

你能解释一下这个神秘的代码吗?

[英]Can you explain this mysterious code?

Found this while debugging C++ code in Embarcadero RAD Studio. 在Embarcadero RAD Studio中调试C ++代码时发现了这一点。 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. 如果有人能提供理智的解释,我将不胜感激。

It's using direct initialization syntax to initialize objPtr to a newly allocated Tobject. 它使用直接初始化语法将objPtr初始化为新分配的Tobject。 For most practical purposes, it's equivalent to Tobject *objPtr = new Tobject(); 对于大多数实际用途,它相当于Tobject *objPtr = new Tobject(); .

This creates an object of type TObject on the heap and stores its location in a TObject pointer called objPtr. 这将在堆上创建一个TObject类型的对象,并将其位置存储在名为objPtr的TObject指针中。 It should be deleted via the line delete objPtr at some point to prevent memory leaks. 它应该在某些时候通过删除objPtr行删除,以防止内存泄漏。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM