内联成员函数两种方式
   实现的时候加inline
   直接在类中给出实现方式

 

内联函数,题号效率,在编译的时候,将代码直接嵌入到调用的地方,

从而减少了函数调用的开销。
 
每调用一次,程序体积就会变大,实际是以空间换时间的例子。
 
 
内联函数仅仅是给编译器一个提示而已,如果函数中有switch,for.编译器
很可能不会将它当成内联函数。
 
 
第一种inline的定义:
 
  1. //Test.h  
  2. # ifndef _TEST_H_  
  3. # define _TEST_H_  
  4.   
  5. class Test  
  6. {  
  7. public:  
  8.      int Add(int a, int b); //内联成员函数  
  9.     //{  
  10.         //return (a+b);  
  11.     //}   
  12. };  
  13.   
  14. # endif  
Test.cpp
 
  1. //Test.cpp 
  2. # include "Test.h" 
  3.  
  4. //inline函数的定义 
  5. int Test::Add(int a, int b) 
  6.     return (a+b); 
main.cpp
 
  1. # include "Test.h" 
  2. # include <iostream> 
  3. using namespace std; 
  4.  
  5. int main(void
  6.     int res; 
  7.     Test t; 
  8.     res = t.Add(2,3); 
  9.      
  10.     cout<< "res = " << res << endl; 
  11.  
  12.     return 0; 
 第二种方法:
直接在类定义中给出实现方法
 
 
  1. //Test.h   
  2. # ifndef _TEST_H_   
  3. # define _TEST_H_   
  4.    
  5. class Test   
  6. {   
  7. public:   
  8.      int Add(int a, int b); //内联成员函数   
  9.     {   
  10.         return (a+b);   
  11.     }    
  12. };   
  13.    
  14. # endif