Details inline functions and function overloads in C++

  • 2020-05-05 11:32:22
  • OfStack

inline functions (inline functions, built-in functions)

Calling a function requires a certain amount of time and space overhead. C++ provides an efficient way to replace function calls with function bodies at compile time, similar to macro expansions in the C language. This type of function that directly inserts the body of the function at the function call is called an inline function (inline function), also known as an inline function or inline function.

Specifying inline functions is as simple as adding the inline keyword to the function definition.

Note: the inline keyword is added when the function is defined, not when the function is declared. Adding inline to the function declaration has no error, but it has no effect

The inline keyword does not work when placed in the function declaration:


inline void swap(int &a, int &b);
void swap(int &a, int &b)
{
  int temp = a;
  a = b;
  b = temp;
}
inline  Keywords should be placed with the function body: 
void swap(int &a, int &b);
inline void swap(int &a, int &b)
{
  int temp = a;
  a = b;
  b = temp;
}

Using inline function can effectively avoid the overhead of function call, the program execution efficiency is higher. The disadvantage of using inline functions is that if the body of the function declared as inline is very large, the executable code of the program compiled by the compiler will become large.

In addition, if there are loops or other complex control structures in the body of a function, it takes much longer to process these complex control structures than it does to call the function, so declaring such functions inline makes little sense and makes compiled executable code longer.

Usually during programming, we declare short functions that are called frequently as inline functions.

It should be noted that an inline declaration of a function is only a suggestion from the programmer to the compilation system, that is, it is a suggestion, not a command. The compilation system is not required to do this once inline is specified. The build system decides whether to do this or not on a case-by-case basis.

A complete example:


#include <iostream>
using namespace std;
int max(int, int, int); // Function declaration, the left end can also add inline
int main( )
{
  int i=10, j=20, k=30, m;
  m = max(i, j, k);
  cout<<"max="<<m<<endl;
  return 0;
}
inline int max(int a, int b, int c) // define max Is an inline function 
{
  if(b>a) a=b;
  if(c>a) a=c;
  return a;
}

Result:


max=30

Since it is specified as a built-in function when defining a function, when the compiler encounters a function call "max(i, j, k)", it replaces "max(i,j, k)" with the code from the body of the max function and replaces the argument with the formal parameter. Thus, line 6 "m=max(i, j, k);" Is replaced by:


  if (j>i) i=j;
  if(k>i) i=k;
  m=i;

The function overloads

In programming, sometimes we want to implement the same type of functionality, but with different details. For example, if you want to find the largest of three Numbers, each time you find the largest number, the data type is different. It may be three integers, three doubles, or three long integers. Programmers often design functions with three different names, with the function prototype


  int max1(int a, int b, int c); // o 3 The largest of the four whole Numbers 
  double max2(double a, double b, double c); // o 3 The largest number of doubles 
  long max3(long a, long b, long c); // o 3 The largest of four long integers 

C++ allows multiple functions to be defined with the same function name with different Numbers and types of arguments. This is the overload of the function (function overloading). That is, to give a function name new meaning, so that a function name can be used.

For the maximum number problem above, you can write the following C++ program.

Find the largest of the three Numbers, ie consider integers, doubles, and long integers, respectively.


#include <iostream>
using namespace std;
int main( )
{
  int max(int a,int b,int c); // Function declaration 
  double max(double a,double b,double c); // Function declaration 
  long max(long a,long b,long c);// Function declaration 
  int i1,i2,i3,i;
  cin>>i1>>i2>>i3; // The input 3 An integer 
  i=max(i1,i2,i3); // o 3 The largest of the four whole Numbers 
  cout<<"i_max="<<i<<endl;
  double d1,d2,d3,d;
  cin>>d1>>d2>>d3; // The input 3 Two doubles 
  d=max(d1,d2,d3); // o 3 The largest of five double - precision Numbers 
  cout<<"d_max="<<d<<endl;
  long g1,g2,g3,g;
  cin>>g1>>g2>>g3; // The input 3 A long integer 
  g=max(g1,g2,g3); // o 3 The largest of four long integers 
  cout<<"g_max="<<g<<endl;
}
int max(int a,int b,int c) // Define o 3 The function of the largest of the integers 
{
  if(b>a) a=b;
  if(c>a) a=c;
  return a;
}
double max(double a,double b,double c)// Define o 3 The function of the largest of two - precision Numbers 
{
  if(b>a) a=b;
  if(c>a) a=c;
  return a;
}
long max(long a,long b,long c) // Define o 3 The function of the largest of the three long integers 
{
  if(b>a) a=b;
  if(c>a) a=c;
  return a;
}

Here's how it works:


185-76567 �  ( The input 3 An integer )
56.87 90.23 -3214.78 �  ( The input 3 A real number )
67854 -912456 673456 �  ( The input 3 A long integer )
i_max=567 ( The output 3 The maximum number of integers )
d_max=90.23 ( The output 3 The maximum of two doubles )
g_max=673456 ( The output 3 The maximum number of long integers )

The bodies of the above three max functions are the same.

In fact, overloaded functions do not require the same body; In addition to allowing different types of parameters, the number of parameters is allowed to vary.

Write a program to find the largest number of two or three integers. If you enter two integers, the program outputs the largest of the two integers, and if you enter three integers, the program outputs the largest of the three integers.


#include <iostream>
using namespace std;
int main( )
{
  int max(int a,int b,int c); // Function declaration 
  int max(int a,int b); // Function declaration 
  int a=8,b=-12,c=27;
  cout<<"max(a,b,c)="<<max(a,b,c)<<endl;// The output 3 The largest of the four whole Numbers 
  cout<<"max(a,b)="<<max(a,b)<<endl; // Outputs the largest of two integers 
}
int max(int a,int b,int c)// this max The function is to find 3 The largest of the four whole Numbers 
{
  if(b>a) a=b;
  if(c>a) a=c;
  return a;
}
int max(int a,int b)// this max The maximum () function is used to find the largest of two integers 
{
  if(a>b) return a;
  else return b;
}

Here's how it works:


max(a, b, c)=27
max(a, b)=8

When the number of parameters of the max function is different, the system finds the matching function according to the number of parameters and calls it.

The number and type of arguments can vary. But you can't just have different types of functions and have the same number of arguments. For example:


  int f(int); // The return value of the function is integer 
  long f(int); // The return value of the function is a long integer 
  void f(int); // The function returns no value 


It is the same form when the function is called, such as "f(10)". The compilation system cannot tell which function to call. There must be at least one difference in the number, type, or order of arguments of an overloaded function, and the return value type of the function may be the same or different.

In the use of overloaded functions, the function of the same name should be the same or similar function, do not use the same function name to achieve completely irrelevant functions, although the program can also run, but the readability is not good, make people puzzling.


Related articles: