C++basic--inline

本文深入探讨了内联函数与宏在C++中的应用及区别,详细讲解了内联函数如何提高代码执行效率,同时对比了宏在代码替换上的局限性。文章还介绍了内联函数在类和虚函数中的使用规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

inline

函数调用都有一定的开销(建立调用,传递参数,跳转到函数代码并返回).

使用宏使代码内联,可以避免这样的开销.另一种方法则是内联函数.

内联函数的编译代码与其他程序代码"内联"起来.

当调用内联函数时,程序使用内联代码代替函数调用.

inline与常规函数

内联函数的运行速度比常规函数稍快.(代价是占用更多内存)

//square.cpp
#include<iostream>
using namespace std;
inline double square(double a){return a*a;}
int main(){
    double a=0.5;
    cout<<"The first square's area is "
        <<square(1.5)<<endl;
    //square(1.5){return 1.5*1.5;}
    cout<<"The second square's area is "
        <<square(1+0.5)<<endl;
    //square(1+0.5){return (1+0.5)*(1+0.5;)}
    cout<<"The third square's area is "
        <<square(++a)<<endl;
    //square(++a){return (a+1)*(a+1);}
    cout<<"now a = "<<a<<endl;
    return 0;
}
The first square's area is 2.25
The second square's area is 2.25
The third square's area is 2.25
now a = 1.5

将函数作为内联函数时要注意,编译器一般不内联包含循环,递归等复杂操作的内联函数.

inline与宏

C语言使用预处理器语句#define来提供宏,这是内联代码的原始实现.

但是,宏不能按值传递,而是通过文本替换来实现的.

//SQUARE.cpp
#include<iostream>
using namespace std;
#define SQUARE(X) ((X)*(X))
int main(){
    double a=1.5;
    cout<<"The first = "<<SQUARE(2.5)<<endl;
    //(2.5)*(2.5)
    cout<<"The second = "<<SQUARE(1+1.5)<<endl;
    //(1+1.5)*(1+1.5)
    cout<<"The third = "<<SQUARE(a++)<<endl;
    //(1.5+1+1)*(1.5+1+1)
    cout<<"now a = "<<a<<endl;
    return 0;
}
The first = 6.25
The second = 6.25
The third = 3.75//1.5*2.5
now a = 3.5

宏SQUARE(++a)中a递增了两次,而内联函数中a只递增一次

inline与类

在类声明中定义的的函数,除了虚函数其他函数都会自动隐式地当成内联函数.类外定义的函数需要显示内联.类声明常将短小地成员函数作为内联函数.

内联函数的特殊规则要求每个使用它们的文件中都对其进行定义.将内联定义放在定义类的头文件中即可实现.

//myhead.hpp
#ifndef MYHEAD_H
#define MYHEAD_H
class stu
{
	public:
	int getid(int a){return a;}//定义即内联
	inline int getage(int b);//声明内联
};
#endif
//stu.cpp
#include"myhead.hpp"
#include<iostream>
using namespace std;
inline int stu::getage(int b){
	return b;
}
int main(){
	stu bob;
	cout<<"his id is "<<bob.getid(111)<<endl;
	cout<<"his age is "<<bob.getage(20)<<endl;
	return 0;
}
his id is 111
his age is 20

virtual与inline

虚函数在不表现多态性时可以内联

内联发生在编译期,而虚函数的多态性表现在运行时.

当编译器知道所调用的对象是哪个类时,虚函数可以内联

//person.cpp
#include<iostream>
using namespace std;
class person
{
    public:
    inline virtual void who()
    {
        cout<<"he is a person"<<endl;
    }
    virtual ~person(){}//为多态基类声明virtual析构函数
};
class stu : public person
{
    public:
    void who() //隐式内联
    {
        cout<<"he is a student"<<endl;
    }
};
int main(){
    person bob;
    bob.who();//bob属于person,可以内联
    person *tom = new stu();
    tom->who();//虚函数通过指针调用,不可内联
    delete tom;
    tom = nullptr;
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值