When should I write the keyword 'inline' for a function/method?

后端 未结 15 2458
清歌不尽
清歌不尽 2020-11-21 06:55

When should I write the keyword inline for a function/method in C++?

After seeing some answers, some related questions:

  • When should I <

15条回答
  •  野性不改
    2020-11-21 07:22

    C++ inline function is powerful concept that is commonly used with classes. If a function is inline, the compiler places a copy of the code of that function at each point where the function is called at compile time.

    Any change to an inline function could require all clients of the function to be recompiled because compiler would need to replace all the code once again otherwise it will continue with old functionality.

    To inline a function, place the keyword inline before the function name and define the function before any calls are made to the function. The compiler can ignore the inline qualifier in case defined function is more than a line.

    A function definition in a class definition is an inline function definition, even without the use of the inline specifier.

    Following is an example, which makes use of inline function to return max of two numbers

    #include 
    
    using namespace std;
    
    inline int Max(int x, int y) { return (x > y)? x : y; }
    
    // Main function for the program
    int main() {
       cout << "Max (100,1010): " << Max(100,1010) << endl;
    
       return 0;
    }
    

    for more information see here.

提交回复
热议问题