Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the benefit of declaring a function outside class in c++? [duplicate]

I am fairly new to C++. I encountered two ways of declaring member functions in c++, ie. Inside class and Outside Class. I searched about the differences and it says the functions that are defined inside class are treated as inline functions.

When I previously read about inline functions it stated that inline is just a request to the compiler to substitute function body instead of calling the functions. But if the function is complex (like recursive, contains static variables, switch etc.) then the compiler will ignore the request. So even if we declare the function inside class if it is complex compiler will ignore the request. Then what is the point of defining functions outside class, if we can just let this decision on compiler itself to automatically do it for us?

like image 740
samarendra chandan bindu Dash Avatar asked Nov 06 '19 16:11

samarendra chandan bindu Dash


Video Answer


1 Answers

Defining a complex member function in a class makes the class definition too complicated and not well-readable.

Bear in mind that you can declare a member function with the function specifier inline without its definition within the class.

Take into account that if the compiler will make non-inline calls of a member function of a class defined within the class nevertheless all other requirements to the function will be the same as for an inline function. For example the function definition with the class definition can be included in many compilation units.

Defining a member function outside a class allows to separate the interface and its realization.

like image 199
Vlad from Moscow Avatar answered Oct 19 '22 01:10

Vlad from Moscow