问题描述
来自 https://timsong-cpp.github.io/cppwp/basic.compound#3 :
- 指向对象或函数的指针(据说该指针指向该对象或函数),或者
- 超出对象末尾的指针([expr.add]),或
- 该类型的空指针值,或
- 无效的指针值.
使用指针显式调用对象的析构函数后,指针具有这四种值中的哪一种?例子:
After using a pointer to explicit call an object's destructor, which of these four kinds of value does the pointer have? Example :
#include <vector>
struct foo {
std::vector<int> m;
};
int main()
{
auto f = new foo;
f->~foo();
// What is the value of `f` here?
}
我不认为它可以是指向对象或函数的指针.不再有指向的对象它不是函数指针.
I don't believe it can be a pointer to an object or function. There is no longer an object to point toand it is not a function pointer.
我不认为它可以是对象末尾的指针.没有任何类型的指针运算,也没有涉及数组.
I don't believe it can be a pointer past the end of an object. There wasn't any sort of pointer arithmetic and no array is involved.
我不认为它可以是空指针值,因为指针不是 nullptr
.它仍然指向对象拥有的存储,您可以使用它执行放置 new
.
I don't believe it can be a null pointer value since the pointer is not nullptr
. It still points to the storage the object had, you could use it to perform placement new
.
我不认为它可以是无效的指针值.无效的指针值与存储持续时间的结束而不是对象生存期相关联. "指针值在表示存储时变为无效到达其存储期限的末尾" .存储仍然有效.
I don't believe it can be an invalid pointer value. Invalid pointer values are associated with the end of storage duration, not object lifetime. "A pointer value becomes invalid when the storage it denotes reaches the end of its storage duration". The storage is still valid.
在我看来,指针没有指针值.我哪里出问题了?
It seems to me like there is no pointer value the pointer could have. Where did I go wrong?
推荐答案
它是指向对象的指针,但是对象不在其生存期内.
It's the pointer to the object, but the object is just not within its lifetime.
这篇关于使用指针显式调用指向对象的析构函数后,指针将持有哪种值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!