Inline functions and virtual functions

Source: Internet
Author: User
Inline functions and virtual functions

The Inline attribute of the function is determined during compilation. However, the nature of virtual is determined during runtime. The two attributes cannot exist at the same time and can only have one choice, the Inline keyword in the file is only a suggestion to the compiler. Whether the compiler adopts the keyword is a matter of the compiler.

1. inline functions are static actions, while virtual functions are dynamic actions, which are in conflict.

2.
We can see some virtual functions like inline functions, because whether a function is an inline function is determined by the compiler rather than the calculation. We can only advise the compiler that a function can be an inline function (Inline keyword), but the compiler has its own judgment rules. Therefore, this situation may occur:

2.1
The functions we declare with Inline do not have inline.

2.2
The function we didn't declare with Inline is inline.

2.3
For inline functions, the compiler still compiles them into an address-based function.

Therefore, the situation is complicated. From the perspective of high-level, it is difficult to judge whether the function is inline. From the perspective of low-level, it is clear that non-inline functions follow the function call mechanism, call is used in the assembly. Inline functions do not have these.

The Inline Function indicates that the function is inline. It is recommended that the Compilation Program directly expand the function code to insert the caller code. this only indicates whether the program will be inline. the compiler will also choose whether to inline based on the internal structure of the function declared as inline, such as whether to include loops, complex function calls, and so on.

1.
There is no doubt that virtual functions will not be inline, because only the runtime of virtual functions can be recognized as which one is called, this is obviously not the same when the code is expanded and inserted during compilation.

2.
Inline has two manifestations (. CPP) indicates that this is called display inline, and the other one is put into the same file as the class declaration and definition, which is called implicit inline, however, as mentioned above, inline is just a prompt. It is up to the compile program to determine whether to inline the prompt.

 

# Include <iostream> using namespace STD; Class A {public: inline virtual void virfun () // The member function of the class is implemented as the callback function in static mode {cout <"I Am a virtual function, and inline does not work! "<Endl;} void F () ;}; inline void a: F () {cout <" I am a real inline function and cannot be set as a virtual function at the same time! "<Endl;} Class B: Public A {public: Virtual void virfun () {cout <" I Am a virtual function in B! "<Endl ;}}; int main () {A * pA; B; Pa = & B; pa-> virfun ();. F ();}

 

PS: How can I check whether a function marked as inline is processed by the compiler as an inline function?. Here we provide a method: Use nm to view the target file that calls the inline function. If the inline function symbol is found, it indicates that it is not used as an inline function. If it is not found, the compiler treats it as an inline function. Because if a function marked as inline is treated as an inline function, the compiler should replace the call Tag directly with the code, so the inline function symbol should not be seen.

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.