C++中内联函数inline

内联函数(inline function与一般的函数不同,不是在调用时发生控制转移而是在编译阶段将函数体嵌入到每一个调用语句中

内联函数(inline function)与编译器的工作息息相关。编译器会将程序中出现内联函数的调用表达式用内联函数的函数体来替换。

Cpp代码复制代码
  1. /** 
  2. *在类里定义的成员函数会被隐含指定为内置函数 
  3. */  
  4.   
  5. #include "stdafx.h"  
  6. #include <iostream>  
  7. #include <string>  
  8.   
  9. using namespace std;   
  10.   
  11. class CStudent   
  12. {   
  13. public:   
  14.     void display()   
  15.     {   
  16.         cout<<"name:"<<name<<endl;   
  17.     }   
  18.     string name;   
  19. };   
  20.   
  21. int main(int argc, char* argv[])   
  22. {   
  23.     CStudent myStudent;   
  24.     myStudent.name="Erin";   
  25.     myStudent.display();   
  26.     return 0;   
  27. }  
  1. /** 
  2. *在类里定义的成员函数会被隐含指定为内置函数 
  3. */  
  4.   
  5. #include "stdafx.h"  
  6. #include <iostream>  
  7. #include <string>  
  8.   
  9. using namespace std;  
  10.   
  11. class CStudent  
  12. {  
  13. public:  
  14.     void display()  
  15.     {  
  16.         cout<<"name:"<<name<<endl;  
  17.     }  
  18.     string name;  
  19. };  
  20.   
  21. int main(int argc, char* argv[])  
  22. {  
  23.     CStudent myStudent;  
  24.     myStudent.name="Erin";  
  25.     myStudent.display();  
  26.     return 0;  
  27. }  
 
Cpp代码复制代码
  1. /** 
  2. *类外定义的函数用inline指定为内置函数 
  3. */  
  4.   
  5. #include "stdafx.h"  
  6. #include <iostream>  
  7. #include <string>  
  8.   
  9. using namespace std;   
  10.   
  11. class CStudent   
  12. {   
  13. public:   
  14.     inline void display();   
  15.     string name;   
  16. };   
  17.   
  18. inline void CStudent::display()   
  19. {   
  20.     cout<<"name:"<<name<<endl;   
  21. }   
  22.   
  23. int main(int argc, char* argv[])   
  24. {   
  25.     CStudent myStudent;   
  26.     myStudent.name="Erin";   
  27.     myStudent.display();   
  28.     return 0;   
  29. }  
  1. /** 
  2. *类外定义的函数用inline指定为内置函数 
  3. */  
  4.   
  5. #include "stdafx.h"  
  6. #include <iostream>  
  7. #include <string>  
  8.   
  9. using namespace std;  
  10.   
  11. class CStudent  
  12. {  
  13. public:  
  14.     inline void display();  
  15.     string name;  
  16. };  
  17.   
  18. inline void CStudent::display()  
  19. {  
  20.     cout<<"name:"<<name<<endl;  
  21. }  
  22.   
  23. int main(int argc, char* argv[])  
  24. {  
  25.     CStudent myStudent;  
  26.     myStudent.name="Erin";  
  27.     myStudent.display();  
  28.     return 0;  
  29. }  
 
Cpp代码复制代码
  1. /** 
  2. *无内置函数 
  3. *既没有在类内定义函数,也没有用inline在类外定义函数 
  4. */  
  5.   
  6. #include "stdafx.h"  
  7. #include <iostream>  
  8. #include <string>  
  9.   
  10. using namespace std;   
  11.   
  12. class CStudent   
  13. {   
  14. public:   
  15.     void display();   
  16.     string name;   
  17. };   
  18.   
  19. void CStudent::display()   
  20. {   
  21.     cout<<"name:"<<name<<endl;   
  22. }   
  23.   
  24. int main(int argc, char* argv[])   
  25. {   
  26.     CStudent myStudent;   
  27.     myStudent.name="Erin";   
  28.     myStudent.display();   
  29.     return 0;   
  30. }  
  1. /** 
  2. *无内置函数 
  3. *既没有在类内定义函数,也没有用inline在类外定义函数 
  4. */  
  5.   
  6. #include "stdafx.h"  
  7. #include <iostream>  
  8. #include <string>  
  9.   
  10. using namespace std;  
  11.   
  12. class CStudent  
  13. {  
  14. public:  
  15.     void display();  
  16.     string name;  
  17. };  
  18.   
  19. void CStudent::display()  
  20. {  
  21.     cout<<"name:"<<name<<endl;  
  22. }  
  23.   
  24. int main(int argc, char* argv[])  
  25. {  
  26.     CStudent myStudent;  
  27.     myStudent.name="Erin";  
  28.     myStudent.display();  
  29.     return 0;  
  30. }  
 

内联函数的优点:
     首先来看函数调用的实质,其实是将程序执行转移到被调用函数所存放的内存地址,将函数执行完后,在返回到执行此函数前的地方。这种转移操作需要保护现场(包括各种函数的参数的进栈操作),在被调用函数代码执行完后,再恢复现场。但是保护现场和恢复现场需要较大的资源开销。
     特别是对于一些较小的调用函数来说(如上边代码中的display()函数),若是频繁调用,函数调用过程甚至可能比函数执行过程需要的系统资源更多。所以引入内联函数,可以让程序执行效率更高。

内联函数的缺点:
      如果调用内联函数的地方过多,也可能造成代码膨胀。毕竟,编译器会把内联函数的函数体嵌入到每一个调用了它的地方,重复地嵌入。
回答: 在C++内联函数inline)是一种特殊的函数,它的定义处增加了inline关键字。内联函数的作用是在函数调用将函数的代码直接插入到调用处,而不是通过函数调用的方式执行。这样可以减少函数调用的开销,提高程序的执行效率。\[1\] 内联函数的定义方式与普通函数相似,但在函数定义处增加了inline关键字。例如,我们可以使用内联函数来实现交换两个数的值: ```cpp inline void swap(int *a, int *b){ int temp = *a; *a = *b; *b = temp; } ``` 在使用内联函数,编译器会将函数的代码直接插入到函数调用处,而不是通过函数调用的方式执行。这样可以避免函数调用的开销,提高程序的执行效率。\[1\] 需要注意的是,内联函数适用于函数体较小且频繁调用的情况。对于函数体较大或者不频繁调用的函数,使用内联函数可能会导致代码膨胀,降低程序的执行效率。因此,需要根据实际情况来决定是否使用内联函数。\[3\] #### 引用[.reference_title] - *1* [【C++内联函数inline](https://blog.csdn.net/Jacky_Feng/article/details/120748428)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [[C++] 内联函数inline 以及 auto关键字 -- C++入门(4)](https://blog.csdn.net/qq_58325487/article/details/124735528)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值