C ++ programming ideology Reading Notes-9 inline functions

Source: Internet
Author: User

1. using macros in C can improve program efficiency, but macros are prone to errors.

In macro definition, it is best to enclose variables in parentheses; otherwise, errors may occur.

There is a difference between a macro and a common function. A common function is a value transfer or address transfer, but a macro is a simple copy. A special parameter is a ++, which makes it easy for a ++ to execute multiple times.

2. In macros, the scope of the class cannot be expressed. Therefore, macros cannot be used in classes. Instead, inline functions are used.

Write the function definition in the class declaration. The default function is the inline function.

Inline functions are similar to common functions and will be checked by the compiler for the parameter list and return values. However, you can directly replace the functions with the function body during calling to reduce the call overhead,

However, whether or not to inline is determined by the compiler. Even if the inline is clearly written, it may not be replaced by inline. The following two cases are clear and cannot be inline:

1. The function body is complex. For example, if a loop exists, the function body cannot be inline. The reason is that the complex function body replacement will weaken or even lead to performance improvement.

2 show or implicitly obtain the function address. The compiler must allocate memory for the function body.

3 forward reference

class F{public:int f() const { return g()+1; }int g() const {return 1;} }

C ++ rules: the inline function in the class takes effect only after the class declaration is complete.

So it is okay to reference undeclared internal functions.

4. Where inline cannot replace macros:

String definition: convert any identifier into a String Array

#define F(s) cout<< #s << endl; s ;

Flag paste:

#define D1(x) int x##one; #define D2(x) int x##two; 

Head. h

#ifndef HEAD_H#define HEAD_H#define F(s) cout<< #s << endl; s ;#define D1(x) int x##one; #define D2(x) int x##two;class Forward{        public:                int f() {return g() +1;}                int g() {return 1;}};#endif

Main. cpp

#include <string>#include <iostream>#include "head.h"using namespace std;int main(){        F(cout << "abc" << endl;);        D1(num);        D2(num);        numone = 100;        numtwo = 200;        F(cout<<numone << ' ' << numtwo << endl;);        Forward f1;        cout << f1.f() << endl;        cout << f1.g() << endl;}

Zhao @ Ubuntu :~ /C ++ work/cpp9 $./A. Out
Cout <"ABC" <Endl;
ABC
Cout <numone <''<numtwo <Endl;
100 200
2
1

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.