C++ provides inline functions to reduce the function call overhead
Inline functions
An inline function is a function that is expanded in line when it is called
When the inline function is called whole code of the inline function gets inserted or substituted at the point of the inline function call
This substitution is performed by the C++ compiler at compile time
An inline function may increase efficiency if it is small
Inline function declaration
1. inline return-type function-name(parameters)
2. {
3. // functioncode
4. }
Inlining is only a request to the compiler, not a command. The compiler can ignore the request for inlining.
Circumstances where the compiler may not perform inlining
If a function contains a loop (for, while and do-while)
If a function contains static variables
If a function is recursive
If a function return type is other than void, and the return statement doesn't exist in a function body
If a function contains a switch or goto statement
Why inline functions are used
When the program executes the function call instruction the CPU stores the memory address of the instruction following the function call, copies the arguments of the function on the stack, and finally transfers control to the specified function
The CPU then executes the function code, stores the function return value in a predefined memory location/register, and returns control to the calling function
This can become overhead if the execution time of the function is less than the switching time from the caller function to called function (callee)
For functions that are large and/or perform complex tasks, the overhead of the function call is usually insignificant compared to the amount of time the function takes to run
For small, commonly-used functions, the time needed to make the function call is often a lot more than the time needed to actually execute the function's code
Advantages of inline functions
Function call overhead doesn't occur
It also saves the overhead of push/pop variables on the stack when a function is called
It also saves the overhead of a return call from a function
When you inline a function, you may enable the compiler to perform context-specific optimization on the body of the function
Such optimizations are not possible for normal function calls
An inline function may be useful (if it is small) for embedded systems because inline can yield less code than the function called preamble and return
Disadvantages of inline functions
The added variables from the inline function consume additional registers
When the inline function body is substituted at the point of the function call, the total number of variables used by the function also gets inserted. So the number of registers going to be used for the variables will also get increased
If you use too many inline functions then the size of the binary executable file will be large, because of the duplication of the same code
Too much inlining can also reduce your instruction cache hit rate, thus reducing the speed of instruction fetch from that of cache memory to that of primary memory
The inline function may increase compile time overhead if someone changes the code inside the inline function then all the calling location has to be recompiled because the compiler would be required to replace all the code once again to reflect the changes, otherwise it will continue with old functionality
Inline functions may not be useful for many embedded systems. Because in embedded systems code size is more important than speed
Friend function
A friend function can be granted special access to private and protected members of a class in C++
They are the non-member functions that can access and manipulate the private and protected members of the class for they are declared as friends
Types of friend functions
Global function
Member function of another class
Features of friend functions
A friend function is a special function in C++ that in spite of not being a member function of a class has the privilege to access the private and protected data of a class
A friend function is a non-member function or ordinary function of a class, which is declared as a friend using the keyword "friend" inside the class. By declaring a function as a friend, all the access permissions are given to the function
The keyword "friend" is placed only in the function declaration of the friend function and not in the function definition or call
A friend function is called like an ordinary function. It cannot be called using the object name and dot operator. However, it may accept the object as an argument whose value it wants to access
A friend function can be declared in any section of the class i.e. public or private or protected
Virtual function
A virtual function (also known as virtual methods) is a member function that is declared within a base class and is re-defined (overridden) by a derived class
When you refer to a derived class object using a pointer or a reference to the base class, you can call a virtual function for that object and execute the derived class's version of the method
Virtual functions ensure that the correct function is called for an object, regardless of the type of reference (or pointer) used for the function call
They are mainly used to achieve Runtime polymorphism
Rules for virtual functions in C++
Virtual functions cannot be static
A virtual function can be a friend function of another class
Virtual functions should be accessed using a pointer or reference of base class type to achieve runtime polymorphism
The prototype of virtual functions should be the same in the base as well as the derived class
They are always defined in the base class and overridden in a derived class. It is not mandatory for the derived class to override (or re-define the virtual function), in that case, the base class version of the function is used
A class may have a virtual destructor but it cannot have a virtual constructor
Limitations of virtual functions
Slower: The function call takes slightly longer due to the virtual mechanism and makes it more difficult for the compiler to optimize because it does not know exactly which function is going to be called at compile time
Difficult to Debug: In a complex system, virtual functions can make it a little more difficult to figure out where a function is being called from
Abstract class
Sometimes implementation of all functions cannot be provided in a base class because we don't know the implementation
Such a class is called an abstract class
We cannot create objects of abstract classes
Pure virtual function
A pure virtual function (or abstract function) in C++ is a virtual function for which we can have an implementation, But we must override that function in the derived class, otherwise, the derived class will also become an abstract class
A pure virtual function is declared by assigning 0 in the declaration