C++中内联函数inline

什么是内联函数???

在函数声明或定义时,将inline关键字加在函数返回类型前面的就是内联函数。

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

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

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

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

class CStudent
{
public:
	void display()
	{
		cout<<"name:"<<name<<endl;
	}
	string name;
};

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

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

class CStudent
{
public:
	inline void display();
	string name;
};

inline void CStudent::display()
{
	cout<<"name:"<<name<<endl;
}

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

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

class CStudent
{
public:
	void display();
	string name;
};

void CStudent::display()
{
	cout<<"name:"<<name<<endl;
}

int main(int argc, char* argv[])
{
	CStudent myStudent;
	myStudent.name="Erin";
	myStudent.display();
	return 0;
}

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

内联函数的缺点:
如果调用内联函数的地方过多,也可能造成代码膨胀。毕竟,编译器会把内联函数的函数体嵌入到每一个调用了它的地方,重复地嵌入。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值