65,211
社区成员
发帖
与我相关
我的任务
分享
#include<iostream>
using namespace std;
class TestRef{
friend ostream& operator<<(ostream& os,TestRef& a);
public:
TestRef(int i,int j):num(i),index(j){}
const TestRef operator+(const TestRef& t)const{
return TestRef(this->num+t.num,this->index+t.index);
}
private:
int num;
int index;
};
ostream& operator<<(ostream& os,TestRef& a){
os<<a.num<<" "<<a.index<<endl;
return os;
}
int main(){
TestRef tr1(2,5);
TestRef tr2(3,7);
TestRef &tr3=tr1;
tr3=tr1+tr2;
cout<<tr3<<endl;
}
int main(){
TestRef tr1(2,5);
TestRef tr2(3,7);
TestRef &tr3=tr1;
tr3=tr1+tr2;
cout<<tr3<<endl;
011E1000 mov ecx,dword ptr [__imp_std::cout (11E2058h)]
011E1006 push esi
011E1007 push dword ptr [__imp_std::endl (11E2038h)]
011E100D mov esi,ecx
011E100F push 0Ch ;5+7=12
011E1011 push 5 ;2+3=5
011E1013 call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (11E2044h)]
011E1019 push eax
011E101A call std::operator<<<std::char_traits<char> > (11E1100h)
011E101F pop ecx
011E1020 mov ecx,eax
011E1022 call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (11E2044h)]
011E1028 mov ecx,eax
011E102A call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (11E2040h)]
011E1030 push dword ptr [__imp_std::endl (11E2038h)]
011E1036 mov ecx,esi
011E1038 call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (11E2040h)]
}
448: #include<iostream>
449: using namespace std;
450:
451:
452: int main()
453: {
00401C60 55 push ebp
00401C61 8B EC mov ebp,esp
00401C63 51 push ecx
454:
455: int a=1024;
456: int &b=a;
457: int k;
458: cin>>k;
00401C64 8B 0D 2C 62 40 00 mov ecx,dword ptr [__imp_std::cin (40622Ch)]
00401C6A 56 push esi ;入栈保护esi
00401C6B 8D 45 FC lea eax,[k]
00401C6E 50 push eax
00401C6F BE 00 04 00 00 mov esi,400h ;在开了/01优化情况下,a的值直接保存在esi中,
;也就是说a的存储空间对应esi寄存器中。
00401C74 FF 15 1C 62 40 00 call dword ptr [__imp_std::basic_istream<char,std::char_traits<char> >::operator>> (40621Ch)]
459: if(k==0)
00401C7A 83 7D FC 00 cmp dword ptr [k],0
00401C7E 75 01 jne main+21h (401C81h)
460: {
461: b++;
00401C80 46 inc esi ;从这来看引用b根本没有分配空间,对引用b的操作直接映射到a对应的
;内存操作,也就是说a b都是esi这块空间的别名,
462: }
463: b++;
464:
465: cout<<a<<endl;
00401C81 FF 35 20 62 40 00 push dword ptr [__imp_std::endl (406220h)]
00401C87 8B 0D 24 62 40 00 mov ecx,dword ptr [__imp_std::cout (406224h)]
00401C8D 46 inc esi
00401C8E 56 push esi
00401C8F FF 15 28 62 40 00 call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (406228h)]
00401C95 8B C8 mov ecx,eax
00401C97 FF 15 30 62 40 00 call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (406230h)]
466: system("pause");
00401C9D 68 FC 37 40 00 push offset string "pause" (4037FCh)
00401CA2 FF 15 D0 62 40 00 call dword ptr [__imp__system (4062D0h)]
00401CA8 59 pop ecx
467: return 0;
00401CA9 33 C0 xor eax,eax
00401CAB 5E pop esi
468: }
00401CAC C9 leave
00401CAD C3 ret
[Quote=引用 3 楼 lingyin55 的回复:]