"C + + base" inline function __jquery

Source: Internet
Author: User
Tags class definition function definition
1. Why Use inline

On most machines, the calling function does a lot of work: Save the Register before the call, restore it on return, copy the arguments, and the program must also move to a new location to execute.


Use inline functions to avoid the overhead of a function call.


Inline instructions are just a suggestion for the compiler, and the compiler can choose to ignore this recommendation.


2. Inline function definition

The definition of an inline function must be visible to the compiler so that the compiler can expand the code of the function in the call point. At this point, only functional prototypes are not enough.

Inline functions may have to be defined more than once in a program, as long as the definition of an inline function appears only once in a source file, and in all source files, the definition must be exactly the same. Placing the definition of an inline function in a header file ensures that when the function is invoked, the same definition is used. and ensure that the definition of the function at the call point is visible to the compiler.


Inline functions should be defined in the header file (recommended).


3. Inline functions in a class

member functions that are defined within a class are automatically treated as inline.

You can define a member as a inline in the class definition body as part of its declaration. Alternatively, you can specify inline on a function definition outside of the class definition body. Specifying inline at the declaration and definition is legal.

An inline member function that is not defined in the class definition body, whose definition is usually placed in the same head file with the class definition.


Class screen
{public
:
    typedef std::string::size_type Index;
	
	Implicitly inline where defined inside the class declaration
	char get () const {return CONTENTS[CUROSR];}
	
	Explicitly declared as inline, willl is defined outside the class declaration
	inline char get (index HT, index WD) c Onst;
	
	Inline not specified in class declaration, but can is defined inline
	index later () const;

Inline declared in the class declaration; No need
to repeat on the definition Char screen::get (index R, index c) cosnt
{
    index row = r* width;
	return Contents[row + c];
}

Not declared as inline into the class declaration, but OK to do inline in definition
inline screen::index Et_cirsot () const
{return
    cursor
}


Reference :effective C + +, clause 30: Thorough understanding of inlining


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.