刚学C++的时候老师就跟我们说内联函数“类似宏替换,使用函数体替换调用处的函数名”,但是我编了一程序,VC6编译,默认的代码优化方式,然后反汇编一下发现貌似是一个不折不扣的普通函数调用, 可以告诉我是什么原因吗? 或者我写的代码有什么问题?
代码如下:
#include <stdio.h>
class myClass
{
public:
myClass(int a, int b);
inline int Add()
{
return m+n;
}
private:
int m;
int n;
};
myClass::myClass(int a, int b):m(a), n(b)
{
}
int main()
{
myClass myPtr(1, 2);
myPtr.Add();
return 0;
}
反汇编代码如下:
00401070 /> \55 PUSH EBP
00401071 |. 8BEC MOV EBP,ESP
00401073 |. 83EC 48 SUB ESP,48
00401076 |. 53 PUSH EBX
00401077 |. 56 PUSH ESI
00401078 |. 57 PUSH EDI
00401079 |. 8D7D B8 LEA EDI,DWORD PTR SS:[EBP-48]
0040107C |. B9 12000000 MOV ECX,12
00401081 |. B8 CCCCCCCC MOV EAX,CCCCCCCC
00401086 |. F3:AB REP STOS DWORD PTR ES:[EDI]
00401088 |. 6A 02 PUSH 2
0040108A |. 6A 01 PUSH 1
0040108C |. 8D4D F8 LEA ECX,DWORD PTR SS:[EBP-8]
0040108F |. E8 80FFFFFF CALL DisasmCl.00401014
00401094 |. 8D4D F8 LEA ECX,DWORD PTR SS:[EBP-8] ;this指针
00401097 |. E8 7DFFFFFF CALL DisasmCl.00401019 ;调用的内联函数
0040109C |. 33C0 XOR EAX,EAX
0040109E |. 5F POP EDI
0040109F |. 5E POP ESI
004010A0 |. 5B POP EBX
004010A1 |. 83C4 48 ADD ESP,48
004010A4 |. 3BEC CMP EBP,ESP
004010A6 |. E8 75000000 CALL DisasmCl.00401120
004010AB |. 8BE5 MOV ESP,EBP
004010AD |. 5D POP EBP
004010AE \. C3 RETN
00401019 $ /E9 92000000 JMP DisasmCl.004010B0 ;调用内联函数过程
004010B0 /> \55 PUSH EBP
004010B1 |. 8BEC MOV EBP,ESP
004010B3 |. 83EC 44 SUB ESP,44
004010B6 |. 53 PUSH EBX
004010B7 |. 56 PUSH ESI
004010B8 |. 57 PUSH EDI
004010B9 |. 51 PUSH ECX
004010BA |. 8D7D BC LEA EDI,DWORD PTR SS:[EBP-44]
004010BD |. B9 11000000 MOV ECX,11
004010C2 |. B8 CCCCCCCC MOV EAX,CCCCCCCC
004010C7 |. F3:AB REP STOS DWORD PTR ES:[EDI]
004010C9 |. 59 POP ECX
004010CA |. 894D FC MOV DWORD PTR SS:[EBP-4],ECX
004010CD |. 8B45 FC MOV EAX,DWORD PTR SS:[EBP-4]
004010D0 |. 8B00 MOV EAX,DWORD PTR DS:[EAX]
004010D2 |. 8B4D FC MOV ECX,DWORD PTR SS:[EBP-4]
004010D5 |. 0341 04 ADD EAX,DWORD PTR DS:[ECX+4]
004010D8 |. 5F POP EDI
004010D9 |. 5E POP ESI
004010DA |. 5B POP EBX
004010DB |. 8BE5 MOV ESP,EBP
004010DD |. 5D POP EBP
004010DE \. C3 RETN
希望高手不吝赐教,感激不尽
[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课