I'm a little confused about inlining member functions. Does all code actually have to be on one line or does it simply mean writing the whole function inside the class rather than from outside using the :: syntax?

I'm a little confused about inlining member functions.

It would help to understand how regular functions typically work. A single definition of the code is created, then calling code jumps into the definition to execute the function. Inlined functions have their bodies essentially pasted directly where the call is so that there's no jump and relatively little bookkeeping overhead. For smaller functions that aren't called in a great many places, this can improve performance.

But beware, because inlined functions can also degrade performance by increasing the code size to the point that it crosses cache lines. Or if the function isn't completely CPU bound, any benefit of inlining can easily disappear.

Does all code actually have to be on one line

No, though the shorter the function is, the higher the chance of it actually being inlined. Note that marking a function as inline (or putting it in the class declaration) is just a hint, the compiler is free to ignore it.

or does it simply mean writing the whole function inside the class rather than from outside using the :: syntax?

You can define the function outside of a class and use the inline keyword to the same effect.

Okay, so inline functions don't actually have to take up only one line. And placing a function inside a class? What's the rule that determins if it's an inline function or not?

What's the rule that determins if it's an inline function or not?

If a member function is defined inside the class definition, it's marked as inline implicitly. If a member function is defined outside the class definition, an explicit inline keyword marks it as inline.

Whether the compiler chooses to actually inline the function depends on the compiler's internal rule set. The short answer is, you simply don't know for sure that a function will be inlined when given the hint. The only way to ensure inlining is to use another method, such as the old #define preprocessor directive.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.