该程序的输出:
#include <iostream>
class c1
{
public:
c1& meth1(int* ar) {
std::cout << "method 1" << std::endl;
*ar = 1;
return *this;
}
void meth2(int ar)
{
std::cout << "method 2:"<< ar << std::endl;
}
};
int main()
{
c1 c;
int nu = 0;
c.meth1(&nu).meth2(nu);
}
是:
method 1
method 2:0
为什么在meth2()
开始时nu
不是1?
nu
、&nu
和c
推入堆栈,然后调用meth1
,将结果推入堆栈,然后调用meth2
;而基于寄存器的调用约定则希望将c
和&nu
加载到寄存器中,调用meth1
,将nu
加载到寄存器中,然后调用meth2
。 - Neil