C++ 中带指针的设置器
Setters with pointers in C++
我正在学习 C++ class,但我很难理解何时需要删除指针。根据我之前的理解,只要不再使用使用 new 创建的指针,我们就需要取消分配内存。另一方面,使用对另一个变量的引用分配的指针不需要它。
对于class的实例变量setter,我们需要它吗?无法知道传递给 setter 的指针是否是用 "new" 创建的。
Test.cpp
int main(){
string name = "john";
string address = "11 blv. hello";
string tel = "514-999-9999";
Date d(1,1,1);
Customer bob(name, address, tel, &d);
bob.printInfo();
Date d2(2,2,2);
bob.setDob(&d2); //Could also be new Date(2,2,2)
bob.printInfo();
return 0;
}
Customer.cpp中的setter:
void Customer::setDob(Date *d){
//delete(dob); Do I need to put it or not?
Customer::dob = d;
}
I'm having a hard time understanding when do I need to delete a pointer.
From my previous understanding, we need to de-allocate memory whenever a pointer that was created with new is not used anymore.
你对这里的理解非常到位。每当您使用 new
表达式分配了一个对象并且您不再使用它时,您需要恰好删除一个指针。
相反,您不得删除未分配给 new
表达式的任何内容,也不得删除仍在使用的任何内容。您也不能删除任何已经删除的内容。
On the other hand, a pointer that is assigned using a reference to another variable doesn't need it.
正确。不仅不需要删除指向变量对象的指针,而且实际上是不应该做的事情。变量不是用 new
表达式创建的。因此,不能删除它们。
In the case of the setter for an instance variable of a class, do we need it?
这取决于该对象的创建方式。如果它是用 new
创建的并且您不打算在其他地方删除它,那么您必须在此处删除它。如果对象不是用 new
创建的,或者如果您确实在其他地方删除了它,那么您不能在此处删除它。
There is no way to know if the pointer that is passed to the setter has been created with "new" or not.
那样的话,这里的指针一定不能删除。否则你可能会删除一些不能删除的东西。换句话说,该函数不得拥有该指针。
如果指向的对象是动态的,那么它必须在其他地方删除:
// For demonstrative purpose only.
// It's never a good idea to use owning bare pointers.
// Use smart pointers.
auto ptr = new Date(2,2,2);
Date d2(2,2,2);
{
Customer bob(name, address, tel, &d);
bob.setDob(&d2); // OK
bob.setDob(ptr); // OK
bob.setDob(new Date(2,2,2)); // Not OK; would leak memory
}
delete ptr;
我看不出为什么 Customer
class 会指向外部 Date
对象作为 dob
(出生日期?)。这似乎应该是 Customer
.
的子对象
我正在学习 C++ class,但我很难理解何时需要删除指针。根据我之前的理解,只要不再使用使用 new 创建的指针,我们就需要取消分配内存。另一方面,使用对另一个变量的引用分配的指针不需要它。
对于class的实例变量setter,我们需要它吗?无法知道传递给 setter 的指针是否是用 "new" 创建的。
Test.cpp
int main(){
string name = "john";
string address = "11 blv. hello";
string tel = "514-999-9999";
Date d(1,1,1);
Customer bob(name, address, tel, &d);
bob.printInfo();
Date d2(2,2,2);
bob.setDob(&d2); //Could also be new Date(2,2,2)
bob.printInfo();
return 0;
}
Customer.cpp中的setter:
void Customer::setDob(Date *d){
//delete(dob); Do I need to put it or not?
Customer::dob = d;
}
I'm having a hard time understanding when do I need to delete a pointer.
From my previous understanding, we need to de-allocate memory whenever a pointer that was created with new is not used anymore.
你对这里的理解非常到位。每当您使用 new
表达式分配了一个对象并且您不再使用它时,您需要恰好删除一个指针。
相反,您不得删除未分配给 new
表达式的任何内容,也不得删除仍在使用的任何内容。您也不能删除任何已经删除的内容。
On the other hand, a pointer that is assigned using a reference to another variable doesn't need it.
正确。不仅不需要删除指向变量对象的指针,而且实际上是不应该做的事情。变量不是用 new
表达式创建的。因此,不能删除它们。
In the case of the setter for an instance variable of a class, do we need it?
这取决于该对象的创建方式。如果它是用 new
创建的并且您不打算在其他地方删除它,那么您必须在此处删除它。如果对象不是用 new
创建的,或者如果您确实在其他地方删除了它,那么您不能在此处删除它。
There is no way to know if the pointer that is passed to the setter has been created with "new" or not.
那样的话,这里的指针一定不能删除。否则你可能会删除一些不能删除的东西。换句话说,该函数不得拥有该指针。
如果指向的对象是动态的,那么它必须在其他地方删除:
// For demonstrative purpose only.
// It's never a good idea to use owning bare pointers.
// Use smart pointers.
auto ptr = new Date(2,2,2);
Date d2(2,2,2);
{
Customer bob(name, address, tel, &d);
bob.setDob(&d2); // OK
bob.setDob(ptr); // OK
bob.setDob(new Date(2,2,2)); // Not OK; would leak memory
}
delete ptr;
我看不出为什么 Customer
class 会指向外部 Date
对象作为 dob
(出生日期?)。这似乎应该是 Customer
.