6.3 内联函数

Posted: March 25, 2011 in C++

/*内联函数实际上是将内联函数复制到调用函数中, 这样当需要调用内联函数的时候就不需要多次跳入和跳出函数.
这样可以提高对于程序的速度和效率.
但是这样可能会扩大调用函数的本身体积,如果是大于1-5行的代码,不要使用内联函数,因为这样可能使函数体积变大,而导致生成文件急剧膨胀.*/

#include <iostream>

inline int Double(int);

using namespace std;

void main()
{
             int target;
             cout<<“Enter a number:”;
             cin>>target;
             cout<<endl;

             target=Double(target); //事实上等同于target=2*target
             cout<<“Target”<<target<<endl;

             target=Double(target);
             cout<<“Target”<<target<<endl;
 
             target=Double(target);
             cout<<“Target”<<target<<endl;
}

int Double(int target)
{
              return 2*target;
}
//虽然避免了在执行程序时跳转和返回的开销, 但是代价可能使执行程序更大.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s