《C++ primer plus》学习笔记——第八章

版权声明:扫我头像就可以向我提问,共同提高:) https://blog.csdn.net/u011436427/article/details/86504877

文章目录

一、C++内联函数

(1)C++区别于C语言,提供了新特性:内联函数,引用传递变量,默认的参数值,函数重载(多态)以及模板函数;
(2)C++内联函数的优势:
在这里插入图片描述
在这里插入图片描述

(3)写内联函数的要求:
(a)在函数声明前加上关键字inline;
(b)在函数定义前加上关键字inline;
(c)内联函数不能递归

(4)什么时候用内联函数呢?
代码量不多,且经常使用
(5)eg如下:
在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>


// inline.cpp -- using an inline function
#include <iostream>
// an inline function definition
inline double square(double x) { return x * x; }
int main()
{
    using namespace std;
    double a, b;
    double c = 13.0;

    a = square(5.0);
    b = square(4.5 + 7.5);   // can pass expressions
    cout << "a = " << a << ", b = " << b << "\n";
    cout << "c = " << c;
    cout << ", c squared = " << square(c++) << "\n";
    cout << "Now c = " << c << "\n";

	system("pause");
	return 0;
}

在这里插入图片描述

说明:
(1)
在这里插入图片描述
(2)内联函数与宏定义的区别
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/u011436427/article/details/86504877
今日推荐