Presentation is loading. Please wait.

Presentation is loading. Please wait.

C++ 程序语言设计 Chapter 8: Inline Functions. Macro  like a function without function overhead  implemented with the preprocessor instead of the compiler.

Similar presentations


Presentation on theme: "C++ 程序语言设计 Chapter 8: Inline Functions. Macro  like a function without function overhead  implemented with the preprocessor instead of the compiler."— Presentation transcript:

1 C++ 程序语言设计 Chapter 8: Inline Functions

2 Macro  like a function without function overhead  implemented with the preprocessor instead of the compiler proper  preprocessor replaces all macro calls directly with the macro code #define FLOOR(x, b) x>=b?0:1

3 problem in C++ with Macro  macros in C++, two problems 1.doesn’t always act like function call. bury difficult-to-find bugs 2.the preprocessor has no permission to access class member data  In C++, we use inline function instead.

4 Preprocessor pitfalls  preprocessor cannot think like compiler (check) #define F (x) (x + 1) f(1) expand to: (x) (x+1) (1) #define FLOOR(x, b) x>=b?0:1 if(FLOOR(a&0x0f, 0x07)) // … if(a&0x0f>=0x07?0:1) #define FLOOR(x, b) (x)>=(b)?0:1 see the example :MacroSideEffects.cpp  no permission to access class member data  In a word, macro is less safety in C++

5 Inline functions  inline function just like an ordinary function, it is expanded in place.  inline function  inline function implicate Any function defined within a class body is automatically inline. inline keyword make a non-class function inline inline int func1(int x) {return ++x}  Define the inline function in header file less inline function occupy less space. No No multiple definition errors (static function)

6 Inlines inside classes automatically  function you define inside a class definition is automatically an inline as we mention before. see the example : Inline.cpp  access function read or change part of the state of an object. please see example : Access.cpp  accessorsmutators  accessors and mutators accessors accessors to read state information from an object. mutators. mutators to change the state of an object. Good manner : Good manner : accessors start with Get, mutator with Set. please see example : Rectangle.cpp, Rectangle2.cpp

7 Inlines & the compiler  Understand inlines compile 1.Check 1.Check the inline function symbol table 1.holds the function type in its symbol table 2.compiler check the inline correctness symbol table 3.brought code for the function body into the symbol table. 2.Prepare for call just like ordinary function. 1.Check the call’s correctness 2.Prepare some implicit conversion. 3.Replace 1.inline code is substituted directly for the function call

8 Limitations of inline  two situations, compiler cannot perform inlining. What does the compiler do. reverts it simply reverts to the ordinary form of a function creating storage creating storage for the function just as it does for a non-inline.  two situations too complicated function is too complicated. sort of looping ; address address of the function is taken implicitly or explicitly  an inline is just a suggestion to the compiler

9 Forward references  if an inline makes a forward reference to a function that hasn’t yet been declared in the class. see the example : EvaluationOrder.cpp  definition states that no inline functions in a class shall be evaluated until the closing brace of the class declaration.

10 Hidden activities in constructors & destructors  Constructors and destructors may have hidden activities, treat them as special, it is much different from the normal function. there’s more going on than meets the eye. see the example : Hidden.cpp

11 Reducing clutter  Two choice to make function inline Define in class body. inline Use the explicit key word inline  Key inline is better way Make the interface clean Please see the example : Noinsitu.cpp


Download ppt "C++ 程序语言设计 Chapter 8: Inline Functions. Macro  like a function without function overhead  implemented with the preprocessor instead of the compiler."

Similar presentations


Ads by Google