Skip to content

What is inline function in C++? Explain with C++ code example

What is inline function in C++ with program example is frequently asked interview question. Answer of inline functions in cpp object oriented programming will include discussion of default inline functions and inline function using keyword inline in a class.

NOTE: In an interview, when inline functions in a class is asked another question can be asked as when to make function inline in C++?

Answer: Inline function in C++ is the function whose complete body get inserted at every places in the program where the function is called before compilation by compiler.

Since C++ inline function code get inserted at every calling points, it consumes memory but it will be faster, as it will avoid time consumption during function call i.e. preparing new stack frame, parameter passing and pushing object’s address on the stack, restoring stack frame back and returning from the function etc.

We use “inline” keyword before a function prototype to make a function inline in C++. for example
inline void A::Counter(){};

Also note that, all functions defined within the class i.e. having function body at the time of declaration itself are by default inline and we can also use keyword “inline” to make function inline. For example

C++ code example :

class A{

	void Counter() { objCount++; } // By default inlined function.
	void Counter(); // explicitly inlined 


// using “inline” keyword to make function inlined.
inline void A::Counter(){

Note that, in C++ inline member function is parsed at compile time and it is a hint to the compiler only, so, it depends upon compiler to consider a function as inline or not.

Published inC++ Basic