inline function members inside a class

C++ClassMethods

C++ Problem Overview


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.

C++ Solutions


Solution 1 - C++

> 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)

Solution 2 - C++

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.

Solution 3 - C++

There are two options to offer to the compiler to make a class function inline:

(1) Defining a function in the declaration of the class (in a header file)

class Human {

public:

	Human(const char* name);
	Human();

    // is implicit inline
	void lookAt(const char* name) const {
		std::cout << "I'm looking at " << name << std::endl;

	}

private:
	char _name[30]; 

}; 

(2) Using the inline keyword explicitly in the definition of the function (in a header file)

    // is explicit inline 
	inline void lookAt(const char* name) const {
		std::cout << "I'm looking at " << name << std::endl;

	}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexDanView Question on Stackoverflow
Solution 1 - C++Alexander PoluektovView Answer on Stackoverflow
Solution 2 - C++JohnView Answer on Stackoverflow
Solution 3 - C++Jan KoesterView Answer on Stackoverflow