是否有任何情况下这两个代码块会做不同的事情?
是的,当你在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