11
C++ 程程程程程程 Chapter 8: Inline Functions

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

Embed Size (px)

Citation preview

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

C++程序语言设计

Chapter 8: Inline Functions

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

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

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

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.

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

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++

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

Inline functions inline functioninline 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 inline function occupy lessless space. NoNo multiple definition errors (static function)

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

Inlines inside classes function you define inside a class definition is

automaticallyautomatically an inline as we mention before. see the example : Inline.cpp access functionaccess function read or change part of the state of an object . please see example : Access.cpp accessorsaccessors and mutatorsmutators

accessorsaccessors 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

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

Inlines & the compiler Understand inlines compile

1.1. CheckCheck the inline function1. holds the function type in its symbol tablesymbol table 2. compiler check the inline correctness3. brought code for the function body into the

symbol tablesymbol table.2. Prepare for call just like ordinary function.

1. Check the call’s correctness2. Prepare some implicit conversion .

3. Replace 1. inline code is substituted directly for the

function call

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

Limitations of inline two situations , compiler cannot perform inlining.

What does the compiler do. it simply revertsreverts to the ordinary form of a function creating storagecreating storage for the function just as it does for a

non-inline. two situations

function is too complicatedtoo complicated. sort of looping ; address address of the function is taken implicitly or explicitly

an inline is just a suggestion to the compiler

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

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.

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

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

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

Reducing clutter Two choice to make function inline

Define in class body. Use the explicit key word inlineinline

Key inline is better way Make the interface cleanPlease see the example :Noinsitu.cpp