C++ 深入了解返回值的一个小示例
/// g++ fn.cpp main.cpp -S -m32 float temp; int main(){
float a=fn1();
// float& b=fn1(); //error
float c=fn2();
float& d=fn2();
}
pushl %ebp
movl %esp, %ebp
andl $-16, %esp
subl $16, %esp
call _Z3fn1v
fstps 4(%esp)
call _Z3fn2v
movl (%eax), %eax
movl %eax, 8(%esp)
call _Z3fn2v
movl %eax, 12(%esp)
movl $0, %eax
leave
ret
float fn1(){ temp=temp+1; return temp; } pushl %ebp movl %esp, %ebp subl $4, %esp flds temp fld1 faddp %st, %st(1) fstps temp movl temp, %eax movl %eax, -4(%ebp) flds -4(%ebp) //堆栈上分配的临时对象(值等于temp) leave ret float& fn2(){ temp=temp+1; return temp; } pushl %ebp movl %esp, %ebp flds temp fld1 faddp %st, %st(1) fstps temp movl $temp, %eax popl %ebp ret
浙公网安备 33010602011771号