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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With