関数定義の前にinline指定子を付ける 例)
#include <iostream> using namespace std; inline int even( int x ) { return !( x % 2 ); } int main() { if( even( 10 ) ){ cout << "10 is even" << endl; } if( even( 11 ) ) { cout << "11 is odd" << endl; } return 0; }
#include <iostream> using namespace std; class samp { int i,j; public: samp( int a, int b ); //defined here,divisible() inlined automatically int divisible(){ return !( i % j ); } }; samp::samp( int a, int b ) { i = a; j = b; } int main() { samp ob1( 10, 2 ); samp ob2( 10, 3 ); if( ob1.divisible() ){ cout << "10 is divisible by 2" << endl; } if( ob2.divisible() ){ cout << "10 is divisible by 3" << endl; } return 0; }
コンストラクタ、デストラクタをインラインで定義できる。 例)
#include <iostream> using namespace std; class samp { int i,j; public: //inline constructor samp( int a, int b ){ i = a; j = b; } int divisible(){ return !( i % j ); } };
参考文献