是否有任何情況下這兩個代碼塊會做不同的事情?
是的,當你在const
方法。目前,與me
調用未定義的行爲。原因如下:
如您所知,當您調用成員函數時,會有一個隱含的指針this
。當函數被標記爲const
時,this
指針爲const
。藉此,例如:
struct foo
{
void method1(void);
void method2(void) const;
int i;
};
含蓄,編譯器生成(順便說一句,這是簡化的):
void foo::method1(foo* this);
void foo::method2(const foo* this) const;
那麼,這兩個機構一樣嗎?
foo* me = (foo*)this;
me->i = 1;
// and
this->i = 1;
答案是這取決於,並且如前所述,它是依賴於const
-ness的功能。在非const
功能,它們是相同的:
void foo::method1(foo* this)
{
foo* me = (foo*)this; // this cast is redundant
me->i = 1;
// ...
this->i = 1;
}
但在const
功能:
void foo::method2(const foo* this) const
{
foo* me = (foo*)this; // uh-oh! acts like const_cast
me->i = 1; // modifying a const_cast'd variable is undefined behavior
// ...
this->i = 1; // wouldn't compile
}
我們最終剝離const
路程。所以,不,它們並不總是一樣的。這是C風格演員的危險:它會找到一種方法。順便說一句,鑄造const
本身不是未定義的行爲;這是所述變量的修改。
雖然在你的問題中有一個棘手的問題:你的代碼不應該編譯。像上面註釋過的代碼一樣,在您的const
方法中,您應該無法修改reference_
。
這是不同的,如果reference_
是mutable
,我猜它可能是(假設你給我們編譯代碼。)在這種情況下,我不能肯定,如果第一個樣品導致不確定的行爲,因爲它是首先是mutable
。我不會冒這個機會。
'references_'是否可變?這是一個重要的細節。 – GManNickG