C + + self-learning-inline functions

Source: Internet
Author: User

Inline functions are an inline extension that is used to eliminate the time overhead of function calls. It is commonly used for frequently executed functions

If you need to perform an operation frequently, you can use inline to define it.

#include "stdafx.h" #include <Windows.h> #include <iostream> #include <fstream> #include < Shappmgr.h> #include <stdio.h>using namespace std;inline bool Isnumber (char ch) {return (ch >= ' 0 ') && ; (Ch <= ' 9 '));} int main () {char a;while ((a = Cin.get ()) = ' \ n ') {if (Isnumber (a)) {cout << "you have entered a number between 0-9" << Endl;} else {cout << "you have entered a number other than 0-9" << Endl;}} return 0;}

 However, inline functions cannot contain complex keywords (such as while,switch), and if so, the compiler ignores these definitions and continues to produce the same calling code for the following calls.

Recursive functions cannot be used for inline functions,

In many cases, inline functions are limited to small, frequently called functions.

Inline functions and macro definitions

    • Macro definitions can replace small function definitions, but are defective
    • The macro simply tells the compiler to override the code and does not check the parameter type
    • Usually cannot actually parse into the function that the code wants
    • The function of a macro can be replaced with an inline function:
      #define MAX (a B) ((a) > (b) a:b))

        

You can replace the inline function with:

inline int MAX (int a,int b) {    return a>b?a:b}

Example

#include "stdafx.h" #include <Windows.h> #include <iostream> #include <fstream> #include < Shappmgr.h> #include <stdio.h>using namespace std;inline int Tobiger (int a, int b) {return a > B? a:b;} inline int Tosmall (int a, int b) {return a < b a:b;} int main () {int a = 10;int B = 11;cout << Tobiger (b) << endl;cout << Tosmall (A, b) << endl;a = a + a;cout << Tobiger (a++, b) << endl;cin.get (); return 0;}

Output:

11

10

20

C + + self-learning-inline functions

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.