`
chenniaoc
  • 浏览: 39900 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

C++ Inline Functions

阅读更多
C++ Inline Functions

In this C++ tutorial, you will learn about Inline function, what is inline function, reason for the need of inline function, what happens when an inline function is written, general format of inline function explained with example.

What is Inline Function?

Inline functions are functions where the call is made to inline functions. The actual code then gets placed in the calling program.

Reason for the need of Inline Function:

Normally, a function call transfers the control from the calling program to the function and after the execution of the program returns the control back to the calling program after the function call. These concepts of function saved program space and memory space are used because the function is stored only in one place and is only executed when it is called. This concept of function execution may be time consuming since the registers and other processes must be saved before the function gets called.

The extra time needed and the process of saving is valid for larger functions. If the function is short, the programmer may wish to place the code of the function in the calling program in order for it to be executed. This type of function is best handled by the inline function. In this situation, the programmer may be wondering “why not write the short code repeatedly inside the program wherever needed instead of going for inline function?” Although this could accomplish the task, the problem lies in the loss of clarity of the program. If the programmer repeats the same code many times, there will be a loss of clarity in the program. The alternative approach is to allow inline functions to achieve the same purpose, with the concept of functions.

What happens when an inline function is written?

The inline function takes the format as a normal function but when it is compiled it is compiled as inline code. The function is placed separately as inline function, thus adding readability to the source program. When the program is compiled, the code present in function body is replaced in the place of function call.

General Format of inline Function:

The general format of inline function is as follows:

    inline datatype function_name(arguments)

The keyword inline specified in the above example, designates the function as inline function. For example, if a programmer wishes to have a function named exforsys with return value as integer and with no arguments as inline it is written as follows:

    inline int exforsys( )

Example:

The concept of inline functions:


    #include <iostream.h>
    int exforsys(int);
    void main( )
    {
    int x;
    cout << “\n Enter the Input Value: ”;
    cin>>x;
    cout<<”\n The Output is: “ << exforsys(x);
    }

    inline int exforsys(int x1)
    {
    return 5*x1;
    }


The output of the above program is:

    Enter the Input Value: 10
    The Output is: 50

The output would be the same even when the inline function is written solely as a function. The concept, however, is different. When the program is compiled, the code present in the inline function exforsys( ) is replaced in the place of function call in the calling program. The concept of inline function is used in this example because the function is a small line of code.

The above example, when compiled, would have the structure as follows:


    #include <iostream.h>
    int exforsys(int);
    void main( )
    {
    int x;
    cout << “\n Enter the Input Value: ”;
    cin>>x;
    //The exforsys(x) gets replaced with code return 5*x1;
    cout<<”\n The Output is: “ << exforsys(x);
    }


When the above program is written as normal function the compiled code would look like below:


    #include <iostream.h>
    int exforsys(int);
    void main( )
    {
    int x;
    cout << “\n Enter the Input Value: ”;
    cin>>x;
    //Call is made to the function exforsys
    cout<<”\n The Output is: “ << exforsys(x);
    }                         

    int exforsys(int x1)
    {
    return 5*x1;
    }



Sponsored Links
分享到:
评论

相关推荐

    C++View C++View C++View

    6. **内联函数和函数重载(Inline Functions and Function Overloading)**:内联函数可以减少函数调用的开销,而函数重载则允许在同一作用域内定义多个同名但参数列表不同的函数。 7. **构造函数与析构函数...

    Visual C++ .NET Optimization with Assembly Code

    Chapter 13 - C++ Inline Assembler and Windows Time Functions Chapter 14 - Using Assembly Language for System Programming in Windows Chapter 15 - Optimizing Procedure-Oriented Applications and ...

    Practical C++ Programming C++编程实践

    Inline Functions Versus Normal Functions Case Study: Optimizing a Color-Rendering Algorithm Programming Exercises Answers to Chapter Questions 18. Operator Overloading Creating a Simple Fixed-Point ...

    ANSI c++ Standard, 1998年的c++标准

    7. 内联函数(Inline Functions):用于优化程序性能,允许编译器将函数体插入到每个调用点。 8. 静态成员(Static Members):属于类而不是类的任何实例,可以实现类级别的变量和函数。 二、标准库 C++ 1998标准...

    Thinking in C++.pdf

    - 内联函数(Inline Functions):解释了内联函数的工作原理及应用场景。 - 引用(References):讨论了引用与指针的区别及其在实际编程中的作用。 - 运算符重载(Operator Overloading):介绍了如何自定义...

    Exceptional C++ & More Exceptional C++

    8. **内联函数与预处理器宏(Inline Functions & Preprocessor Macros)**:内联函数用于优化函数调用,而预处理器宏则常用于代码替换。书中警告了过度使用宏可能导致的问题,并提倡使用内联函数和C++11的lambda...

    Prentice.Hall.C++.for.Business.Programming.2nd.Edition.2005.chm

    Special Topics: Friends, Operator Overloading, Macros, and Inline Functions 730 Section 15.1. friend Functions 731 Section 15.2. Overloading Basic Arithmetic Operators 738 Section 15.3. ...

    Think in C++ 英文版(含卷一、卷二)

    9: Inline Functions 10: Name Control 11: References & the Copy-Constructor 12: Operator Overloading 13: Dynamic Object Creation 14: Inheritance & Composition 15: Polymorphism & Virtual Functions 16: ...

    C++程序设计教学课件:CHAPTER 4 FUNCTIONS

    此外,本章还将涉及一些高级主题,如内联函数(`inline functions`),默认参数(`default arguments`)和函数重载(`function overloading`)。内联函数是一种优化技术,用于减少函数调用时的开销,通过在编译时...

    C++ 17 标准手册_C++_

    9. **返回类型推断的改进(Return Type Deduction for Functions, RTDF)** 除了构造函数和成员函数,C++ 17也允许普通函数的返回类型推断,如`auto`关键字。 10. **并行算法(Parallel Algorithms)** C++ 17...

    《深度探索C++对象模型》(Stanley B·Lippman[美] 著,侯捷 译)

    4.5 Inline Functions 形式对数(Formal Arguments) 局部变量(Local Variables) 第5章 构造、解构、拷贝 语意学(Semantics of Construction,Destruction,andCopy) 纯虚拟函数的存在(Presence of a Pure ...

    C++面向对象高效编程

    此外,还可能涉及性能优化技巧,如减少不必要的计算、使用内联函数(inline functions)、避免数据冗余、理解编译器优化等。 总之,《C++面向对象高效编程》是一本全面介绍C++面向对象编程实践的书籍,适合有一定...

    C++程序设计语言 中文特别版(全)

    7. **内联函数(Inline Functions)** 和 **函数重载(Function Overloading)**:内联函数用于优化小规模的函数调用,减少函数调用的开销;函数重载则允许在同一作用域内定义多个同名但参数列表不同的函数。 8. **...

    Seamless R and C++ Integration

    The Rcpp package provides R functions and a C++ library facilitating the integration of R and C++. R data types (SEXP) are matched to C++ objects in a class hierarchy. All R types are supported ...

    Exceptional C++中文版

    10. 性能优化(Performance Optimization):C++提供了许多工具和技巧进行性能优化,如inline函数、内联展开、函数重载和运算符重载等。 通过《Exceptional C++》的学习,读者不仅能够掌握C++的高级特性,还能培养...

    Google C++ Style Guide(Google C++编程规范)高清PDF

    Header Files The #define Guard Header File Dependencies Inline Functions The -inl.h Files Function Parameter Ordering Names and Order of Includes Scoping Namespaces Nested Classes Nonmember, Static ...

    Ruminations On C++

    3. **内联函数**:内联函数(inline functions)可以避免函数调用的开销,当函数体较小时,内联可以显著提升性能。 4. **常量表达式**:C++11引入了constexpr关键字,允许在编译时期计算某些表达式的值,从而在运行...

    c++ ANSI/IEC标准

    - **内联函数(Inline Functions)**:提高函数调用效率。 - **运算符重载(Operator Overloading)**:允许为自定义类型定义运算符行为。 - **函数名重载(Function Name Overloading)**:允许多个同名但参数不同...

Global site tag (gtag.js) - Google Analytics