The two possibilities are not at all equivalent:
- In the first,
f()
is a member function of node
.
- In the second,
f()
is a free (or namespace-scope) function. (Note also that the signature of two f()
are different.)
Now note that, in the first style, f()
is an inline
member function. Defining a member function inside the class body makes it inline. Although inlining is not guranteed, it is just a hint to the compiler. For functions with small bodies, it may be good to inline them, as it would avoid function call over head. However, I have never seen that to be a make-or-break factor.
If you do not want or if f()
does not qualifiy for inlining, you should define it outside the class body (probably in a .cpp file) as:
int node::f() { /* stuff here */ }
If memory usage is a problem in your code, then most probably the above topics are not relevant. Exploring the following might give you some hint
- Find the sizes of all classes in your program. Use
sizeof
to find this information, e.g. sizeof( node)
- Find what is the maximum number of objects of each class that your program is creating.
Using the above two series of information, estimate worst case memory usage by your program
Worst case memory usage = n1 * sizeof( node1 ) + n2 * sizeof( node2 ) + ...
If the above number is too high, then you have the following choices:
- Reducing the number of maximum instances of classes. This probably won't be possible because this depends on the input to the program, and that is beyond your control
- Reduce the size of each classes. This is in your control though.
How to reduce the size of the classes? Try packing the class members compactly to avoid packing.