例题8.1-演示了内联函数的技术

来源:互联网 发布:68淘宝小号网是真的吗 编辑:程序博客网 时间:2024/05/18 23:26
// 8-例题-8.1.cpp : 定义控制台应用程序的入口点。
//
//要使用内联函数必须采取下述措施之一:1.在函数声明前加上关键字inline 2.在函数定以之前加上关键字inline
//inline工具是C++的新增的特性。C语言使用预处理器语句#define来是提供宏---内联函数代码的原始实现
//内联函数是按值传递参数的,而宏不是传递参数实现的,而是通过文本替换来实现的
//例如:#define square(X) X*X
// a=square(5.0);is replaced by a=5.0*5.0;
// b=square(4.5+5.5);is replaced by b=4.5+5.5*4.5+5.5;
// d=square(c++);is replaced by c++*c++;
// 上面只有第一个是正常运行的,其余两个都不能正常运行。
// 在这里可以指出:如果使用C语言的宏执行了类似函数的功能,应考虑将他们转化为C++内联函数
//
#include "stdafx.h"
#include <iostream>


inline double square(double x){return x*x;};


int _tmain(int argc, _TCHAR* argv[])
{
using namespace std;


double a,b;
double c=13.0;


a=square(5.0);
b=square(4.5+7.5);


cout<<"a="<< a << ",b = "<< b <<endl;
cout<<"c= "<< c ;
cout<<", c squared= "<<square(c++)<<endl;
cout<<" Now c= "<< c<<endl;


system("pause");
return 0;
}

0 0
原创粉丝点击