简体   繁体   English

关于新操作员的操作

[英]About the operations for the new operator

If I have a user defined default constructor in my Test class, and what operations will done by using the following statement:如果我在我的测试 class 中有一个用户定义的默认构造函数,并且使用以下语句将执行哪些操作:

Test *test = new Test; //there is no () after new Test

First, does the user default constructor means "constructor without Parameters"?首先,用户默认构造函数是否意味着“没有参数的构造函数”? for example:例如:

class Test {
     public:
             Test() {
              // do something here
             }
}

so the new Test;所以新的测试; means the complier will call the Test() constructor in class Test();表示编译器将调用 class Test() 中的 Test() 构造函数; and perform the operations inside it, and allocate memory in heap for class Test object?并执行其中的操作,并在堆中分配 memory 用于 class 测试 object?

And what about the *test"? where is it? in heap or stack? can anyone explain me? And what about那么*test“呢?它在哪里?在堆还是堆栈中?谁能解释我?那又如何?

Test test = new Test();//with () this time

what kind of constructor will be called in this case?在这种情况下会调用什么样的构造函数?

"Default constructor" means a constructor that can be called without parameters. “默认构造函数”是指可以不带参数调用的构造函数。 It may have default parameters:它可能有默认参数:

class Test {
public:
    Test(int x = 42); // Still default constructor. 
                      // Can be called as Test() and as Test(int);
};

The new operator will call operator new to allocate memory, and provided the allocation succeeds, then call one or more constructors. new operator 会调用operator new来分配 memory,如果分配成功,则调用一个或多个构造函数。 In this case it will call the default one to construct your object.在这种情况下,它将调用默认值来构造您的 object。

More than one constructor will be called if your object has a base class.如果您的 object 具有基础 class,则将调用多个构造函数。

The Test* itself, test , will reside on the stack in this case.在这种情况下, Test*本身test将驻留在堆栈中。

Default constructor means the one which can be called without any parameters which includes constructors with parameters having all default values.默认构造函数是指可以在没有任何参数的情况下调用的构造函数,其中包括具有所有默认值的参数的构造函数。

The statement Test *test = new Test allocates the memory for object of Test on heap and calls the default constructor for it.语句Test *test = new Test为Test on heap 的object 分配memory 并为其调用默认构造函数。

Does the user default constructor means "constructor without Parameters"?用户默认构造函数是否意味着“没有参数的构造函数”?
Yes, Default constructor means the constructor without any parameters.是的,默认构造函数是指没有任何参数的构造函数。 A constructor is a special & only member function for a class which has same name as the class.构造函数是与 class 同名的 class 的特殊且唯一的成员 function。
The compiler generates a default constructor(constructor without any parameters) if your code needs one.如果您的代码需要,编译器会生成一个默认构造函数(没有任何参数的构造函数)。

What does Test *test = new Test; Test *test = new Test;是什么? do?做?
It allocates memory on the heap of size = size of class Test and calls the default constructor Test() .它在 size = size of class Test 的堆上分配 memory 并调用默认构造函数Test()

* And what about the test"? where is it? in heap or stack? can anyone explain me? *那么测试“呢?它在哪里?在堆还是堆栈中?谁能解释我?
*test is a pointer variable of the type Test on the stack which points to a dynamically allocated chunk of memory of the size of class Test on the Heap *teststackTest类型的指针变量,它指向动态分配的 memory 块,大小为 class Heap测试

A default constructor means a constructor that can be called without arguments. default constructor是指可以在没有 arguments 的情况下调用的构造函数。 It is used to initialize the parameters of any object of a class, not necessarily pointers to objects of that class.它用于初始化 class 的任何 object 的参数,不一定是指向该 class 对象的指针。 So,所以,

Test t;

would call the default constructor Test::Test() .将调用默认构造函数Test::Test() Note that t is an object and not pointer to object.请注意, t是 object,而不是指向 object 的指针。 Some languages also have default constructors that are automatically generated when explicit constructors are not defined and over-ridden when constructors are defined.某些语言还具有默认构造函数,这些默认构造函数在未定义显式构造函数时自动生成,并在定义构造函数时被覆盖。

The new operator in C++ allocates memory to the pointer from the heap and initializes it using the constructor. C++ 中的new运算符将 memory 分配给堆中的指针,并使用构造函数对其进行初始化。

HTH,高温下,
Sriram.斯里拉姆。

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

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