Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inheritance and inline?

I have been doing a lot of reading recently and whilst covering inheritance (and virtual functions) I keep stumbling across the "inline" keyword. Now I know what inline is in the normal sense- compiler may replace function calls with the exact code. However, the number of times I have seen it mentioned with regards to inheritance- is there some special reason for using inline within inheritance? I don't understand why it keeps being mentioned....

What extra role does an inline function have within inheritance/derived classes/virtual functions?

like image 855
user997112 Avatar asked Mar 04 '13 22:03

user997112


People also ask

Can inline function be inherited?

Yes, using inline on virtual functions is a waste of time. A virtual function has to be called through a virtual function table, which is made of function pointers. An inline function cannot be called by a pointer. It has to exist as a real function.

What is inline in oops?

An inline function is one for which the compiler copies the code from the function definition directly into the code of the calling function rather than creating a separate set of instructions in memory. This eliminates call-linkage overhead and can expose significant optimization opportunities.

What is inline function example?

Example. In the following class declaration, the Account constructor is an inline function. The member functions GetBalance , Deposit , and Withdraw aren't specified as inline but can be implemented as inline functions. In the class declaration, the functions were declared without the inline keyword.

What is inline in C?

In an inline function, a function call is replaced by the actual program code. Most of the Inline functions are used for small computations. They are not suitable for large computing. An inline function is similar to a normal function. The only difference is that we place a keyword inline before the function name.


1 Answers

Yes, using inline on virtual functions is a waste of time. A virtual function has to be called through a virtual function table, which is made of function pointers. An inline function cannot be called by a pointer. It has to exist as a real function.

There are some exceptions. Where the caller knows the exact object type it can skip the virtual function table entirely.

Overuse of the virtual keyword can result in very slow code. Where the compiler might be able to inline and optimize three or four small function calls, with virtual functions it has to do actual function calls, making no assumptions about memory or register state between calls.

like image 65
Zan Lynx Avatar answered Sep 28 '22 18:09

Zan Lynx