gpt4 book ai didi

Why is it illegal to take the address of the "this" pointer?(为什么获取“This”指针的地址是非法的?)

转载 作者:bug小助手 更新时间:2023-10-25 18:34:16 31 4
gpt4 key购买 nike



I try to find address of this pointer, but this code is showing a strange
error:

我试图找到此指针的地址,但此代码显示了一个奇怪的错误:



#include <iostream>
using namespace std;
class Base
{
public:
void test()
{
void *address_of_this =&this;
cout<<address_of_this<<endl;
}
};

int main()
{ Base k;
k.test();

return 0;
} //error non-lvalue in unary'&'


Can you explain this error ?

Also point that what is illegal in taking address of this?

你能解释一下这个错误吗?还指出,拿这个地址什么是违法的?


更多回答

Try adding a space after the = sign

尝试在=符号后添加空格

优秀答案推荐

this is a pointer containing the address to the "current object". It is not a variable that is stored somewhere (or could even be changed), it is a special keyword with these properties.

这是一个包含指向“当前对象”的地址的指针。它不是存储在某个地方(甚至可以更改)的变量,而是具有这些属性的特殊关键字。



As such, taking its address makes no sense. If you want to know the address of the "current object" you can simply output:

因此,取它的地址是没有意义的。如果您想知道“当前对象”的地址,只需输出:



std::cout << this;


or store as

或存储为



void* a = this;


Quoting the 2003 C++ standard:

引用2003年的C++标准:



5.1 [expr.prim] The keyword this names a pointer to the object for which a nonstatic member function (9.3.2) is invoked. ... The type of the expression is a pointer to the function’s class (9.3.2), ... The expression is an rvalue.


5.3.1 [expr.unary.op] The result of the unary & operator is a pointer to its operand. The operand shall be an lvalue or a qualified_id.



To put it simply, & requires an lvalue. this is an rvalue, not an lvalue, just as the error message indicates.

简单地说,&需要一个左值。正如错误消息所示,这是一个右值,而不是左值。



this refers to the current object by using it's address.

它通过使用当前对象的地址来引用该对象。



In your problem, there are two errors:

在您的问题中,有两个错误:




  • this is not an lvalue.

    这不是左值。



    The & requires an lvalue. lvalues are those that can appear on on the left-hand side of an assignment (variables, arrays, etc.).

    &需要左值。左值是那些可以出现在赋值左侧的值(变量、数组等)。



    Whereas this is a rvalue. rvalues can not appear on the left-hand side (addition, subtraction, etc.).

    然而,这是一个正确的价值。右值不能出现在左侧(加法、减法等)。



    Reference: C++ Rvalue References Explained.

    参考:C++右值参考解释。


  • A hidden error which I'd like to also mention is thus:

    我还想提到的一个隐藏错误是:



    address_of_this is actually receiving an address of an address.

    Address_of_This实际上是在接收一个地址的地址。



    Basically, &this is translated into something like &&object or &(&object).

    基本上,&这被翻译成类似&&Object或&(&Object)之类的东西。




Basically, think of this as &object (but only to remember because it is not that true).

基本上,将其视为&Object(但只是为了记住,因为它并不是那么正确)。



this is a prvalue.

这是原价。


In other words, it's computed on the fly when needed, and not stored in the memory otherwise. Thus it doesn't have a persistent address/location for you to take.

换句话说,它是在需要时动态计算的,而不是存储在内存中。因此,它没有一个持久的地址/位置供您使用。


更多回答

Finally.. the voice of reason.

终于..。理性的声音。

In standardese: this is an rvalue (and not of class type), so it doesn't have an address.

在标准语言中:这是一个右值(并且不是类类型),所以它没有地址。

In Standardese again, this is prvalue - pure-rvalue.

同样地,在标准中,这是prValue纯rValue。

It isn't very clear, what you wanna imply in your 2nd error.

不是很清楚,你想在你的第二个错误中暗示什么。

That something does not have a persistent address/location for one to take need not be reason enough not to take its address. A vector could get reallocated and .data() does offer its current address .

某物没有持久的地址/位置供人取用不一定是不取其地址的充分理由。一个向量可以被重新分配,.data()确实提供了它的当前地址。

@Tryer Ultimately, inability to take addresses of rvalues is an arbitrary language limitation, desiged to prevent you from shooting yourself in the foot. Nothing would stop us from defining & to materialize prvalues into xvalues, which actually exist in memory, and then allowing & to work on xvalues. It would just be hard to use safely, because the resulting pointer would become dangling at the end of the full-expression that produced it. Here's an interesting example: gcc.godbolt.org/z/Mvqvc7836

@Tryer最终,无法接受右值地址是一种武断的语言限制,旨在防止你搬起石头砸自己的脚。没有什么可以阻止我们定义&将prValue物化为xValue,它实际上存在于内存中,然后允许&对xValue进行操作。只是很难安全地使用它,因为产生的指针会在生成它的完整表达式的末尾变得悬空。下面是一个有趣的例子:gcc.Goldbolt.org/z/Mvqvc7836

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