简体   繁体   English

它是如何工作的,测试* pObj = new Test(); 因为构造函数不返回任何东西

[英]How does it work, Test *pObj = new Test(); as constructor does not return anything

I am trying to get better at c++. 我正在努力提高C ++的水平。 I have a Test class and below code in main(). 我有一个Test类,下面的代码位于main()中。

Test *pObj = new Test();

If we debug by steping one by one instruction, First it goes to new function to allocate memory, then it calls constructor. 如果我们通过一步一步的指令进行调试,首先进入新函数分配内存,然后调用构造函数。 Then it comes back to main() function. 然后返回到main()函数。 As we all know, constructor does not return anything. 众所周知,构造函数不会返回任何内容。 In that case, how come pObj will have the pointer which is allocated by new ? 在那种情况下,pObj为何会有new分配的指针? Any idea how it is implemented by compiler ? 知道编译器是如何实现的吗?

When you use a new expression the compiler generates code to allocate memory and then call the constructor on the allocated memory to create a new object. 使用new表达式时,编译器会生成代码以分配内存,然后在分配的内存上调用构造函数以创建新对象。 If successful, it returns a pointer to the new object. 如果成功,它将返回一个指向新对象的指针。

Constructors have no return values, the compiler just adds a call to the constructor on a piece of memory where it needs the new object to be constructed. 构造函数没有返回值,编译器只是在需要构造新对象的一块内存上添加了对构造函数的调用。 It's not necessary for the constructor to return the location of the object, the code already knews where the object must be; 构造函数没有必要返回对象的位置,因为代码已经知道对象必须在哪里。 it (effectively) passed it to the constructor. 它(有效地)将其传递构造函数。

是的。构造函数没有返回任何内容。但是new操作符构造并返回了由pObj指向的Test类实例的分配的内存地址。

The return value you should care about is the one 'returned' by new , in that case the address of the newly allocated object. 您应该关心的返回值是new所返回的值,在这种情况下,新分配的对象的地址。

new can be implemented by a call to malloc for example, which truly return the address ( edit: see comments bellow ). 例如, new可以通过调用malloc来实现,它真正返回了地址( 编辑:请参见bellow注释 )。

I think, i got the answer. 我想,我找到了答案。 new operator allocates memory and makes a call to constructor. new运算符分配内存并调用构造函数。 It will be passed this pointer as the same pointer that new allocated. 它将与新分配的指针一样传递给该指针。 Once constructor returns, control comes back to new operator implementation of compiler.(Not the main() function). 构造函数返回后,控制权将返回到编译器的新运算符实现。(不是main()函数)。 Then, once new returns, as it gives the allocated pointer, pObj will have that pointer. 然后,一旦新的返回,因为它给出了分配的指针,pObj将拥有该指针。

new and delete are operators . new和delete是运算符 By default the new operator allocates the memory for the object, and returns the pointer to that memory. 默认情况下, 操作符为对象分配内存,并将指针返回到该内存。 A constructor is then called if one exists for the object being created, based on the syntax of what comes after the new operator. 然后,根据新运算符之后的语法,如果要创建的对象存在一个构造函数,则调用该构造函数。 The object is already allocated before the constructor is called, so the constructor doesn't need to return anything. 在调用构造函数之前已经分配了对象,因此构造函数不需要返回任何内容。 It is essentially a void method that is called after allocation. 本质上是分配后调用的void方法。

The global new operator is often implemented as a function, such as void* new(size_t s) , where s is the size of the object to be allocated. 全局new运算符通常实现为一个函数,例如void * new(size_t s) ,其中s是要分配的对象的大小。 The compiler figures the size of the object to be created under the hood and passes it to new, returns the pointer to the allocated memory, and then calls the constructor. 编译器计算出要在引擎盖下创建的对象的大小,并将其传递给new,将指针返回到分配的内存,然后调用构造函数。

You can override the new operator either globally or an a per-class basis to add your own behavior. 您可以全局或按类覆盖新运算符,以添加自己的行为。

$5.3.4/1 - "If the entity is a non-array object, the new-expression returns a pointer to the object created. If it is an array, the new-expression returns a pointer to the initial element of the array." $ 5.3.4 / 1-“如果实体是非数组对象,则new-expression返回指向创建的对象的指针。如果它是数组,则new-expression返回指向数组的初始元素的指针。 ”

Note the new expression and operator new are not one and same thing. 注意new表达式和new运算符不是一回事。

$5.3.4/8- "If the allocated type is a non-array type, the allocation function's name is operator new and the deallocation function's name is operator delete. If the allocated type is an array type, the allocation function's name is operator new[] and the deallocation function's name is operator delete[]." $ 5.3.4 / 8-“如果分配的类型是非数组类型,则分配函数的名称为operator new,而释放函数的名称为operator delete。如果分配的类型为数组类型,则分配函数的名称为operator new [],解除分配函数的名称为运算符delete []。”

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

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