Placing Functions inside class vs outside

I see that a function can be coded directly in side a class--or--a model/header can be defined inside the class an the body of the function defined outside the class using double-dots to associate the body with the proper function.

Is there a preference here? It is not clear how memory is allocated, inline-assembler generated etc.

thanks
fritz
To my knowledge, it makes no difference for non-templated functions.
Templated functions must be defined within the class declaration.
There are exceptions to this, but they're ugly.

For very short non-templated functions (1-2 liners) or for a class that has a small number of functions, I will define them in the class declaration. Anything more than that and I will create a separate .cpp file.
OK-so I guess it is only a convenience/syntax thing with no processor operational issues.
I always prefer everything in the class but I can understand how this may make them very large in some cases.
Thanks
Fritz
Another couple considerations:

1) If you're building a .lib or .dll for use by others, the .h file is your public "contract" of how to interface to your library. You typically want to hide your implementation, so all the functions go in the .cpp file.

2) Placing all the functions in the .cpp can minimize rebuilds in a large project. If you make a change to a function in a .cpp file, only that file needs to be recompiled. If you make a change to the .h file, every .cpp file that includes that header must be recompiled.
Functions that are defined inside class scope are implicitly inline and ODR (One Definition Rule) is implicitly also accomplished.

Usually to satisfy ODR you put the inline keyword on your functions, however compiler is free not to inline your function regardless of where this function is defined.

As you can see the inline keyword has double meaning, conclusion is:

If you define your functions outside class scope they are not inline by default,
If you define a function inside class scope it is implicitly inline for ODR.
Any function to be a candiate for inline function must be defined in header file (.h file)

ODR is always guaranteed, however inlining is not regardless of inline keyword.

Keep in mind that function which is defined inside class can be marked as inline (the keyword is not redundant there like some believe it is),
also keep in mind that template function is also subject to inlining and inline keyword, regardless if defined in or outside class scope.
For example, template function defined in header outside class can be marked as inline,
and that keyword is also not redundant there.

The only difference between template and naked function is that you can't put template definition into source file.
If you do, then your template class or function looses some of it's generic behavior by design.
In particular you'll have to explicitly instatiate stuff ahead of time.

What is ODR (One Definition Rule)?
Basically a rule such that function is not defined multiple times, ex. ODR is broken if function is defined in header without inline keyword, see also:
https://en.wikipedia.org/wiki/One_Definition_Rule

EDIT:
OK-so I guess it is only a convenience/syntax thing with no processor operational issues.

There is performance impact in that your function can be selected for inline expansion.
This also brings memory use overhead, however small\big it may be.
Last edited on
and inline is frustrating, it can be slower or faster and its hard to predict which you will get. You can test it without moving it if your compiler has some sort of force-inline code (which still only works if the function CAN be inline).
OK and thank you for all the deliberation. As expected there are "fine point" not always documented that come to the forefront.

cheers
Fritz
Topic archived. No new replies allowed.