gpt4 book ai didi

c++ - 对象生存期内 `this`指针的值是否恒定?

转载 作者:行者123 更新时间:2023-12-01 09:03:29 25 4
gpt4 key购买 nike

在特定对象的生存期内,this指针的值是否保证是恒定的?我无法想象情况会发生变化,但是不知道我是否没有错过任何东西。

最佳答案

Is the value of this pointer guaranteed to be constant during a lifetime of a particular object?



正如用户 Aconcagua所说: this指针的值始终是在其上调用了on1的对象的地址的值。因此,问题等同于:

Can an object change its memory address over life time?


根据 lifetime 2的定义,这是不可能的。对象的生存期在获取对象的存储之时或之后开始,而在其释放之前终止。

1) [class.this]/1

In the body of a non-static ([class.mfct]) member function, the keyword this is a prvalue whose value is a pointer to the object for which the function is called.


2) [basic.life]/1 (重点是我的)

The lifetime of an object or reference is a runtime property of the object or reference. A variable is said to have vacuous initialization if it is default-initialized and, if it is of class type or a (possibly multi-dimensional) array thereof, that class type has a trivial default constructor.The lifetime of an object of type T begins when:

  • storage with the proper alignment and size for type T is obtained, and
  • its initialization (if any) is complete (including vacuous initialization) ([dcl.init]), except that if the object is a union member or subobject thereof, its lifetime only begins if that union member is the initialized member in the union ([dcl.init.aggr], [class.base.init]), or as described in [class.union].

The lifetime of an object o of type T ends when:

  • if T is a non-class type, the object is destroyed, or
  • if T is a class type, the destructor call starts, or
  • the storage which the object occupies is released, or is reused by an object that is not nested within o ([intro.object]).

关于c++ - 对象生存期内 `this`指针的值是否恒定?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59749312/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com