C++basic--inline

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

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;
}
(add-to-list 'magic-fallback-mode-alist '(buffer-standard-include-p . c++-mode)) (c-add-style "my-style" '("stroustrup" (indent-tabs-mode . nil) ; use spaces rather than tabs (c-basic-offset . 8) ; indent by four spaces (c-offsets-alist . ( (inline-open . 0) ; custom indentation rules (brace-list-open . 0) (statement-case-open . +) ))) ) (defun my-c++-mode-hook () (c-set-style "my-style") ; use my-style defined above (auto-fill-mode) ;(c-toggle-auto-hungry-state 1) (c-toggle-electric-state 1) ;(c-toggle-auto-newline 1);这个就是遇到了{};之后,自己开新行的 (c-toggle-hungry-state 1) ;(c-toggle-syntactic-indentation 1) ;(define-key c++-mode-map (kbd ":") 'semantic-ia-complete-symbol-menu) ;(define-key c++-mode-map (kbd ".") 'semantic-ia-complete-symbol-menu) (define-key c++-mode-map (kbd "C-c C-c") 'kill-ring-save);copy (define-key c++-mode-map (kbd "C-v") 'yank);paste (define-key c++-mode-map (kbd "C-x x") 'kill-region);cut ;(define-key c++-mode-map (kbd "C-c C-c") 'kill-ring-save);copy ;(define-key c++-mode-map (kbd "C-v") 'yank);paste (setq tab-width 8 indent-tabs-mode nil) ) (add-hook 'c++-mode-hook 'my-c++-mode-hook) (add-hook 'objc-mode-hook 'my-c++-mode-hook) ;--------------------------------传说中的施特劳斯特卢普的C++代码风格 --------------------------------------- ;----------------------------------C风格的对齐----------------------------- (defconst my-c-style '((c-tab-always-indent . t) (c-comment-only-line-offset . 4) (c-hanging-braces-alist . ((substatement-open after) (brace-list-open))) (c-hanging-colons-alist . ((member-init-intro before) (inher-intro) (case-label after) (label after) (access-label after))) (c-cleanup-list . (scope-operator empty-defun-braces defun-close-semi)) (c-offsets-alist . ((arglist-close . c-lineup-arglist) (substatement-open . 0) (case-label . 4) (block-open . 0); (defun-block-intro . 0) (statement-block-intro . 8) (substatement . 8) (knr-argdecl-intro . -))) (c-echo-syntactic-information-p . t) ) "My C Programming Style" ) ;; Customizations for all of c-mode, c++-mode, and objc-mode
05-13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值