Hello fl!
On Wednesday, October 7, 2015 at 9:14:13 AM UTC-4, fl wrote:
> Hi,
> When I read a tutorial code, I feel a little surprise about an inline function
>
> inline std::ostream&
> operator<<(std::ostream &os, const Query &q)
> {
> return q.display(os);
> }
> ...
I will comment on the "inline" part of your question:
(But, yes, this is a "free" function, i.e., not on a member
function, and that's okay.)
One practical reason to inline a free function is that you
can then include the function's implementation in a header
file. (This may or may not be relevant to your tutorial
or use case.)
I find this a convenience for simple little classes:
point.h:
#include <ostream>
struct Point {
double x = 0.0;
double y = 0.0;
};
inline std::ostream& operator<<(std::ostream &os, const Point p) {
return os << '(' << p.x << ", " << p.y << ')';
}
You can now include point.h in multiple translation units
(multiple .cpp files) without getting a multiple-definition
error at link time.
Normally one would be advised to put more complicated
implementation is a separate .cpp file, but for this
kind of trivial little helper function, it's convenient
to just put everything into one header file and be done
with it (especially in example or tutorial code).
Best.
K. Frank