Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

inline function members inside a class

Tags:

c++

methods

class

I know that declaring a function (normal function not a method inside a class) as inline is a good practice when the function definition is small for performance and it save time for the compilation. But how about inline methods inside a class I don't understand the concept of inline methods inside a class? How to define them and how they work.

like image 531
AlexDan Avatar asked Feb 21 '12 00:02

AlexDan


People also ask

Which function of a class are called inline function?

Explanation: Inline are functions that are expanded when it is called. The whole code of the inline function gets inserted/substituted at the point of call. In this, they help in reducing the function call overheads. Also they save overhead of a return call from a function.

How is inline member function defined outside the body of class?

If you define a member function outside of its class definition, it must appear in a namespace scope enclosing the class definition. You must also qualify the member function name using the scope resolution ( :: ) operator.

Are functions inline by default?

Thus template functions are inline by default.

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.


2 Answers

but how about inline methods inside a class ?

Both syntaxes for inlining functions (using explicit inline and defining member-function inside class definition) provides only hint about inlining for compiler. From performance point of view, they are equal.

In case of defining a member-function inside a class declaration, the readability of the latter should be of your main concern: it really hurts to litter class interface with multiple line of implementation details. So avoid doing that if your member-function is more than one statement: return stuff or simple forwarding should be OK, but usually no more than that.

class MyClass { public:     int f() const { return m_i; }     int g() const;  private:     int m_i; };  inline int MyClass::g() const {     return m_i; }  // both member-functions behave equally (except for naming) 
like image 162
Alexander Poluektov Avatar answered Oct 18 '22 06:10

Alexander Poluektov


Specifying a function/procedure as inline inside a class is hinting to the compiler that instead of creating code to call the function and pass parameters, the contents of the function should be placed at the point of call.

It can improve performance of the compiled binary when it becomes more efficient to execute the function without having to pass parameters. It can also be a detriment to performance because repeating the code that would have been in the function at every call location can cause bloat which lessens the liklihood that your code will be found in faster cache memory.

like image 24
John Avatar answered Oct 18 '22 04:10

John