C++ 仅设置对象指向 nullptr 的指针
c++ setting only pointer of object to nullptr
假设我有一个数组a
它包含指向类B
对象的指针。假设a[0]
指向对象c
。 如果我现在设置a[0] = nullptr
,如果c
是在堆栈上构建的并且a[0]
是唯一引用c
的东西,那么c
的destructor
是否会被调用并因此c
销毁。
如果你引用常规指针(Obj* ptr),那么c的d'tor就不会因为赋值而被调用。如果有类似 - 的代码,可能会调用它 -
... // Some code and initialization before
{
Obj c;
a[0] = &c;
a[0] = NULL; //c isn't affected in any way by this assignment
} //End of scope. **c's d'tor is called here**
虽然,如果在堆上分配了 c,它的内存会泄漏
相关文章:
- 将指针设置为"nullptr"并不能防止双重删除?
- C++默认情况下,指针类型数组的元素是否保证初始化为 nullptr?
- 空指针常量 (nullptr)、空指针值和空成员指针值之间有什么区别?
- 检查模板中 nullptr 的函数指针,了解任何类型的可调用对象
- CURLOPT_INTERLEAVEFUNCTION回调函数始终接收 nullptr 作为用户数据指针
- boost::spirit指针属性是用nullptr初始化的吗?
- unique_ptr<T>::get() 返回的指针在原始unique_ptr被销毁后不是 nullptr
- 在C++中将指针数组的所有元素设置为 nullptr
- 以原子方式交换指针与 nullptr
- 指向抽象类的指针数组:指向 nullptr 或不指向 nullptr (C++)
- 仅当指针不是 nullptr 时才调用方法
- C++链表指针始终为 nullptr
- 为什么分配了 nullptr 的指针可以调用成员函数?
- 空向量占用的空间是否与指向当前设置为 nullptr 的类型的指针一样多
- 如何使用nullptr初始化同一类对象的静态指针数组?
- 为什么我不能使用 nullptr 初始化自动推导的指针?
- std::unique_ptr 是否在其析构函数中将其基础指针设置为 nullptr?
- 我可以得到指向 nullptr 的指针的指针吗,它是否有效
- C++ 仅设置对象指向 nullptr 的指针
- 在类ctor中的默认nullptr指针给出了无法解析的外部符号c++