The inline function and function overload _c language in C + +

Source: Internet
Author: User
Tags function prototype

inline functions (in-line functions, built-in functions)

Calling a function requires a certain amount of time and space overhead. C + + provides a way to improve efficiency by replacing function calls at compile time with function bodies, similar to macro expansion in the C language. This function, which is directly embedded in function calls, is called an inline function, or an inline function or an inline function.

The way to specify inline functions is simple, just add the inline keyword when you define a function.

Note: Add the inline keyword when the function is defined, not when the function is declared. Add inline when function declaration key although there is no error, but there is no effect

The inline keyword does not work at the function declaration:

inline void swap (int &a, int &b);
void swap (int &a, int &b)
{
  int temp = A;
  A = b;
  b = temp;
}
The inline keyword should be put together with the function body:
void swap (int &a, int &b);
inline void swap (int &a, int &b)
{
  int temp = A;
  A = b;
  b = temp;
}

The use of inline functions can effectively avoid the overhead of function calls, and the program execution efficiency is higher. The disadvantage of using inline functions is that if the function body declared as an inline function is very large, then the compiler's executable code will become large.

In addition, if there are loops or other complex control structures in the function, this time it takes much more time to process these complex control structures than the function calls, so declaring these functions as inline functions will make the compiled executable code longer.

Usually in the process of programming, we will declare some of the frequently called short functions as inline functions.

It should be explained that the inline declaration of a function is only a suggestion by the programmer to the compiling system, that is, it is recommended, not prescriptive. Not once specified as inline, the compilation system must do so. The compilation system will decide whether to do so depending on the circumstances.

A complete example:

#include <iostream>
using namespace std;
int max (int, int, int); function declaration, the left side 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 as inline function
{
  if (b>a) a=b;
  if (c>a) a=c;
  return A;
}

Run Result:

Max=30

Because it is specified as a built-in function when the function is defined, the compilation system, when it encounters the function call "Max (I, J, K)", replaces "Max (I,j, K)" With the code of the Max function body, substituting the argument for the parameter. In this way, the program line 6th "M=max (I, J, K);" is replaced by:

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

Function overload

In programming, sometimes we want to achieve the same kind of function, but some details are different. For example, if you want to find the biggest of them from 3, and each time you ask for the maximum number of different types of data, it might be 3 integers, 3 doubles, or 3 long integers. Programmers often design a function of 3 different names, and the function prototype is:

  int max1 (int a, int b, int c); Find the largest of 3 integers
  double max2 (double A, double b, double c);//Find the largest of the 3 double digits long
  max3 (long A, long B, long C);//find 3 long integers The biggest person

C + + allows you to define multiple functions with the same function name, which is different in number and parameter types. This is the overload (function overloading) of the functions. That is, a function name is redefined to give it a new meaning, so that a function name can be used more.

You can write the following C + + program for the maximum number of questions above.

"Example" asks for the maximum number of 3 digits (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; Enter 3 integer i=max (I1,I2,I3);
  Find the largest of 3 integers cout<< "i_max=" <<i<<endl;
  Double d1,d2,d3,d; cin>>d1>>d2>>d3; Enter 3 double-precision number D=max (D1,D2,D3);
  Find the biggest of the 3 doubles cout<< "d_max=" <<d<<endl;
  Long g1,g2,g3,g; cin>>g1>>g2>>g3; Enter 3 Long integer g=max (G1,G2,G3);
Find the largest of 3 long integers cout<< "g_max=" <<g<<endl;
  the int max (int a,int b,int c)//defines the function {if (b>a) a=b that asks for the largest of 3 integers;
  if (c>a) a=c;
return A;
  Double Max (double a,double b,double c) defines the function {if (b>a) a=b that asks for the largest of the 3 double-precision numbers;
  if (c>a) a=c;
return A;
  Long Max (Long A,long b,long C)//defines the function {if (b>a) a=b that asks for the largest of 3 long integers;
  if (c>a) a=c;
return A; }

The operating conditions are as follows:

185-76567↙ (enter 3 integers)
56.87 90.23-3214.78↙ (enter 3 real numbers)
67854-912456 673456↙ (enter 3 long integers)
i_max=567 (Output the maximum of 3 integers) c4/>d_max=90.23 (output maximum of 3 double digits)
g_max=673456 (maximum value for 3 long integers)

The function bodies of the 3 max functions in the previous example are the same.

In fact, overloaded functions do not require the same function body; In addition to allowing parameter types to be different, the number of arguments is allowed to differ.

"Example" writes a program that asks for the maximum number of two integers or 3 integers. If you enter two integers, the program outputs the maximum number of these integers, and if you enter 3 integers, the program prints out the maximum number of those 3 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;//output the largest of 3 integers
  cout<< "max (a,b) =" <<max (a,b) <<endl; Output maximum of two integers
}
int max (int a,int b,int c)//The function of this Max function is to find the maximum of 3 integers
{
  if (b>a) a=b;
  if (c>a) a=c;
  return A;
}
int max (int a,int b)//The function of this Max function is to find the maximum of two integers
{
  if (a>b) return A;
  else return B;
}

   

The operating conditions are as follows:

Max (A, B, c) =27
Max (A, b) =8

Two times the number of arguments to the Max function is different, the system finds the matching function based on the number of parameters and calls it.

The number and type of arguments can be different. However, you cannot have only the same type of function and the number and type of arguments. For example:

  int f (int); function return value is integral type
  long f (int);//function return value is long integer
  void f (int);//function has no return value


Is the same form when the function is called, such as "F (10)". The compilation system cannot discriminate which function should be called. There must be at least one difference in the number of arguments, parameter types, or parameter order of an overloaded function, and the function return value type can be the same or different.

When using overloaded functions, the functions of the same name should be the same or similar, do not use the same function name to achieve completely irrelevant function, although the program can also run, but not readable, makes people inexplicable.

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.