問(wèn)題描述
如標(biāo)題所述,我想知道'this'
指針的類型.
As mentioned in the title, I would like to know about the type of 'this'
pointer.
我正在處理一個(gè)項(xiàng)目,我觀察到 'this'
指針的類型是 "ClassName * const this"
在使用 VC++ 2008 的 Windows 上.好吧,我想知道使 this 指針成為常量指針的需要/要求是什么.謝謝.
I'm working on a project and I observed that the type of 'this'
pointer is "ClassName * const this"
on windows using VC++ 2008. Well I would want to know what is the need/requirement to make the this pointer a constant pointer. Thanks.
推薦答案
這個(gè)指針的類型是 ClassName *
或者 const ClassName *
,取決于它是否是在 ClassName
類的非常量或常量方法中檢查.指針 this
不是左值.
The type of this pointer is either ClassName *
or const ClassName *
, depending on whether it is inspected inside a non-const or const method of the class ClassName
. Pointer this
is not an lvalue.
class ClassName {
void foo() {
// here `this` has `ClassName *` type
}
void bar() const {
// here `this` has `const ClassName *` type
}
};
您上面提到的觀察具有誤導(dǎo)性.指針this
是不是左值,這意味著它不可能有ClassName * const
類型,即它不可能有const
在 *
的右邊.指針類型的非左值不能是 const 或非常量.C++ 語(yǔ)言中根本沒(méi)有這樣的概念.您觀察到的必須是特定編譯器的內(nèi)部怪癖.形式上,這是不正確的.
The observation you mentioned above is misleading. Pointer this
is not an lvalue, which means that it cannot possibly have ClassName * const
type, i.e. it cannot possible have a const
to the right of the *
. Non-lvalues of pointer type cannot be const or non-const. There's simply no such concept in C++ language. What you observed must be an internal quirk of the specific compiler. Formally, it is incorrect.
這里是語(yǔ)言規(guī)范中的相關(guān)引用(重點(diǎn)是我的)
Here are the relevant quotes from the language specification (emphasis mine)
9.3.2 this 指針
在非靜態(tài)(9.3)成員函數(shù)體中,關(guān)鍵字 this 是一個(gè)純右值表達(dá)式,其值為對(duì)象的地址該函數(shù)被調(diào)用.成員函數(shù)中this的類型X 類是 X*.如果成員函數(shù)被聲明為 const,則類型這是 const X*,如果成員函數(shù)被聲明為 volatile,則this 的類型是 volatile X*,并且如果聲明了成員函數(shù)const volatile,this 的類型是 const volatile X*. [ 注意:因此在const 成員函數(shù),調(diào)用該函數(shù)的對(duì)象通過(guò) const 訪問(wèn)路徑訪問(wèn).——結(jié)尾說(shuō)明]
In the body of a non-static (9.3) member function, the keyword this is a prvalue expression whose value is the address of the object for which the function is called. The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const volatile X*. [ Note: thus in a const member function, the object for which the function is called is accessed through a const access path. —end note ]
<小時(shí)>
在 C++98/C++03 時(shí)代,一些編譯器使用內(nèi)部實(shí)現(xiàn)技巧是毫無(wú)價(jià)值的:他們將他們的 this
指針解釋為常量指針,例如ClassName *const
在類 ClassName
的非常量方法中.這顯然有助于他們確保 this
的不可修改性.眾所周知,GCC 和 MSVC 已經(jīng)使用了該技術(shù).這是一個(gè)無(wú)害的技巧,因?yàn)樵谡Z(yǔ)言級(jí)別 this
不是左值,并且它的常量是不可檢測(cè)的.額外的 const
通常只會(huì)在編譯器發(fā)出的診斷消息中顯示出來(lái).
It is worth nothing that back in the C++98/C++03 times several compilers used an internal implementational trick: they interpreted their this
pointers as constant pointers, e.g. ClassName *const
in a non-constant method of class ClassName
. This apparently helped them to ensure non-modifiablity of this
. GCC and MSVC are known to have used the technique. It was a harmless trick, since at language level this
was not an lvalue and its constness was undetectable. That extra const
would generally reveal itself only in diagnostic messages issued by the compiler.
然而,隨著 C++11 中右值引用的出現(xiàn),可以在 this
的類型上檢測(cè)這個(gè)額外的 const
.例如,以下代碼在 C++11 中有效
However, with the advent of rvalue references in C++11 it became possible to detect this extra const
on the type of this
. For example, the following code is valid in C++11
struct S
{
void foo() { S *&&r = this; }
};
然而,在仍然使用上述技巧的實(shí)現(xiàn)中,它通常無(wú)法編譯.GCC 從此放棄了該技術(shù).MSVC++ 仍然使用它(從 VS2017 開始),這阻止了上述完全有效的代碼在 MSVC++ 中編譯.
Yet it will typically fail to compile in implementations that still use the aforementioned trick. GCC has since abandoned the technique. MSVC++ still uses it (as of VS2017), which prevents the above perfectly valid code from compiling in MSVC++.
這篇關(guān)于“this"指針的類型的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!