Inline function depth inquiry - pretreatment, const, static and sizeof (IV)

table of Contents

 

Why introduce an inline function?

Difference inline functions and macros defined

Why inline function can replace the macro definitions and macros than good?

With respect to the macro inline functions unique usage scenarios

Doubt

Why can not all functions are defined as inline functions?


Why introduce an inline function?

inline function introduced aim is to make up for the shortcomings of the macro definition, while a good inherits the advantages of macros. Alternative macro definition with the inline functions to address efficiency issues procedure function calls.

Difference inline functions and macros defined

  • Inline functions are expanded and then compile time, the macro is expanded in the pre-compile time.
  • At compile time, inline function is directly embedded into the object code and the macro just a simple text replacement.
  • Acer is not a function, but an inline function is a function.
  • Inline function is a function because of their own, such as can be done, the type of detection, such as the statement is correct compilation, but not macros, macros can only be replaced by mechanical.
  • Macro is not inline function stable, ambiguous prone to ambiguity

Why inline function can replace the macro definitions and macros than good?

  • Inline functions and macros, can be directly defined in part into the object code to go, no call overhead, very high efficiency
  • Inline functions and the like, is a function, the compiler when you call it, it will go to the type of test parameters inline functions, the statement is correct and other conditions, eliminating the risk of calling an error, and these are not the macro.
  • Since it is a function inline function, then you can use the class member objects accurately, and macros can not (because the macro can not be this pointer in the right place) .

With respect to the macro inline functions unique usage scenarios

Since it is a function inline function, then you can use the class member objects accurately, and macros can not (because the macro can not be this pointer in the right place) .

Such as the following, use inline functions can be invoked to protect the private members of the class:

#include <stdio.h>
#include <iostream>

class test
{
public:
	void setnum(int num);

private:
	int num;
};

inline void test::setnum(int i)
{
	printf("i=%d\n",i);
}

int main()
{
	test A;
	A.setnum(5);
	system("pause");
	return 0;
}

Run as follows: 

Doubt

After my test a little, ah macro can also make changes to follow this!

#include <stdio.h>
#include <iostream>

#define Pr(i) num = i

class test
{
public:
	void sets(int i)
	{
		Pr(i);
	}
	void prsets()
	{
		printf("%d\n", num);
	}

private:
	int num;

};

int main()
{
	test A,B;
	A.sets(5);
	A.prsets();

	B.sets(3);
	B.prsets();
	A.prsets();

	system("pause");
	return 0;
}

Run as follows: 

 

Why can not all functions are defined as inline functions?

The use of only one, to prevent too much overhead:

  • If the function code is a relatively long member, the inline function will consume excessive
  • Union consumption function if recurring, likewise, also make the inside too large
  • If the constructor or destructor is set to inline functions, take care, construction, destructor will have "secretly" conduct operations, for example, not only will the current constructor executes again, they will be Inherited constructor is also executed again, this is the case, it would greatly increase the cost!

Fortunately, when the compiler to inline functions, will first be inspected, can be as complete and efficient implementation by an inline function, can not, then it is automatically converted into ordinary function to perform.

 

 

 

 

 

 

 

 

 

 

 

Published 271 original articles · won praise 8 · views 10000 +

Guess you like

Origin blog.csdn.net/qq_17846375/article/details/104943213