理解这段涉及指针、数组和 new 运算符的代码
Understanding this code involving pointers, arrays, and the new operator
所以这是原始代码:
class IntegerArray {
public:
int *data;
int size;
};
int main() {
IntegerArray arr;
arr.size = 2;
arr.data = new int[arr.size];
arr.data[0] = 4; arr.data[1] = 5;
delete[] a.data;
}
将arr.data = new int[arr.size]
移至构造函数后,变为
class IntegerArray {
public:
int *data;
int size;
IntegerArray(int size) {
data = new int[size];
this->size = size;
}
};
int main() {
IntegerArray arr(2);
arr.data[0] = 4; arr.data[1] = 5;
delete[] arr.data;
}
我完全不知道代码要做什么。对于
IntegerArray(int size) {
data = new int[size];
this->size = size;
}
int 大小是否与 class IntegerArray 中声明的 int 大小相同?
难道data = new int[size]
只是告诉我们data指向int大小的数组输出,而new说数组的大小是可变的?
难道this-> size = size
只是一个指针,告诉我们构造函数的size值刚好等于class的size参数吗?
为什么在 IntegerArray arr(2) 之后还要提到 arr.data[0]
和 arr.data[1]
?他们似乎不遵循构造函数,但我可能太累了无法理解那部分。
this->size = size //the fist size is your member variable which is inside IntegerArray. the 2nd size is the size you give over to the Method
data = new int[size]; // here you are creating a new array with the size of "size"
方法 IntegerArray(int size) 是构造方法,每个对象只能调用一次(即使在创建对象时)
int main() //the startpoint of your program
{
IntegerArray arr(2); //you are creating a new object with a size of 2
arr.data[0] = 4; arr.data[1] = 5; //because of your array is public, you can call it direct from outside (you sould define it as private ore protected). arr.data[0] is the first item of the array, arr.data[1] is the 2nd item
delete[] arr.data;
}
删除[] arr.data;应该在 class...
的析构函数中
IntegerArray(int size) {
data = new int[size];
this->size = size;
}
Is int size just the same as ...
这个int size
:
IntegerArray(int size) {
^^^^^^^^
是构造函数的参数
Does data = new int[size]
just tell us ...
new int[size]
动态分配一个数组,其中包含 size
个 int
个对象。 data
然后分配指针指向新创建的数组。
Is this-> size = size
just a pointer ...
没有。 this
是一个特殊的指针,在构造函数中指向正在构造的对象。 this->size
是这里声明的成员变量:
class IntegerArray {
public:
int *data;
int size;
^^^^^^^^
完整的表达式this->size = size
将作为构造函数参数的值size
赋值给成员变量size
。
Why are arr.data[0]
and arr.data[1]
even mentioned after IntegerArray arr(2)
?
构造函数不初始化数组的内容(数组中的整数对象)。提到的代码确实为它们分配了一个值。
所以这是原始代码:
class IntegerArray {
public:
int *data;
int size;
};
int main() {
IntegerArray arr;
arr.size = 2;
arr.data = new int[arr.size];
arr.data[0] = 4; arr.data[1] = 5;
delete[] a.data;
}
将arr.data = new int[arr.size]
移至构造函数后,变为
class IntegerArray {
public:
int *data;
int size;
IntegerArray(int size) {
data = new int[size];
this->size = size;
}
};
int main() {
IntegerArray arr(2);
arr.data[0] = 4; arr.data[1] = 5;
delete[] arr.data;
}
我完全不知道代码要做什么。对于
IntegerArray(int size) {
data = new int[size];
this->size = size;
}
int 大小是否与 class IntegerArray 中声明的 int 大小相同?
难道data = new int[size]
只是告诉我们data指向int大小的数组输出,而new说数组的大小是可变的?
难道this-> size = size
只是一个指针,告诉我们构造函数的size值刚好等于class的size参数吗?
为什么在 IntegerArray arr(2) 之后还要提到 arr.data[0]
和 arr.data[1]
?他们似乎不遵循构造函数,但我可能太累了无法理解那部分。
this->size = size //the fist size is your member variable which is inside IntegerArray. the 2nd size is the size you give over to the Method
data = new int[size]; // here you are creating a new array with the size of "size"
方法 IntegerArray(int size) 是构造方法,每个对象只能调用一次(即使在创建对象时)
int main() //the startpoint of your program
{
IntegerArray arr(2); //you are creating a new object with a size of 2
arr.data[0] = 4; arr.data[1] = 5; //because of your array is public, you can call it direct from outside (you sould define it as private ore protected). arr.data[0] is the first item of the array, arr.data[1] is the 2nd item
delete[] arr.data;
}
删除[] arr.data;应该在 class...
的析构函数中IntegerArray(int size) { data = new int[size]; this->size = size; }
Is int size just the same as ...
这个int size
:
IntegerArray(int size) {
^^^^^^^^
是构造函数的参数
Does
data = new int[size]
just tell us ...
new int[size]
动态分配一个数组,其中包含 size
个 int
个对象。 data
然后分配指针指向新创建的数组。
Is
this-> size = size
just a pointer ...
没有。 this
是一个特殊的指针,在构造函数中指向正在构造的对象。 this->size
是这里声明的成员变量:
class IntegerArray {
public:
int *data;
int size;
^^^^^^^^
完整的表达式this->size = size
将作为构造函数参数的值size
赋值给成员变量size
。
Why are
arr.data[0]
andarr.data[1]
even mentioned afterIntegerArray arr(2)
?
构造函数不初始化数组的内容(数组中的整数对象)。提到的代码确实为它们分配了一个值。