本文介绍了所以什么是“这个”的类型。 ?为什么是“这个”不是左值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说对象是

  class A {
public:void Silly(){
this = 0x12341234;
}



我知道我会得到编译器错误'这不是一个左值。 但是那不是一个暂时的。那么什么是this的假设声明?



编译器:在Mac上的GCC 4.2编译器。

对于某些类X,这个有类型 X * this; 你不允许赋值它,所以即使它实际上没有类型 X * const this ,它的行为几乎就像防止赋值去。正式来说,它是一个 prvalue ,这是一个类似于整数文本的类,因此尝试分配它大致相当于尝试分配一个不同的值到'a' 10



>早期 C ++,这个是一个左值 - 分配给处理一个对象的内存分配,类似于重载 new delete 当时还支持)。


Say the object is

class A {
public :  void Silly(){
    this = 0x12341234;
}

I know I will get compiler error ' "this" is not a lvalue.' But then it is not a temporary either. So what is the hypothetical declaration of "this" ?

Compiler : GCC 4.2 compiler on mac.

解决方案

For some class X, this has the type X* this;, but you're not allowed to assign to it, so even though it doesn't actually have the type X *const this, it acts almost like it was as far as preventing assignment goes. Officially, it's a prvalue, which is the same category as something like an integer literal, so trying to assign to it is roughly equivalent to trying to assign a different value to 'a' or 10.

Note that in early C++, this was an lvalue -- assigning to this was allowed -- you did that to handle the memory allocation for an object, vaguely similar to overloading new and delete for the class (which wasn't supported yet at that time).

这篇关于所以什么是“这个”的类型。 ?为什么是“这个”不是左值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-03 18:12