Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A function can be defined as an inline type?

53 views
Skip to first unread message

fl

unread,
Oct 7, 2015, 9:14:13 AM10/7/15
to
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);
}

It is called here:

Query q;
cout << "\nExecuting Query for: " << q << endl;

class Query is defined as:

class Query {
public:
Query(const std::string&); // builds a new WordQuery

// copy control to manage pointers and use counting
Query(const Query &c): q(c.q), use(c.use) { ++*use; }
~Query() { decr_use(); }
Query& operator=(const Query&);

// interface functions: will call corresponding Query_base operations
std::set<TextQuery::line_no>
eval(const TextQuery &t) const { return q->eval(t); }
std::ostream &display(std::ostream &os) const
{ return q->display(os); }
private:
Query(Query_base *query): q(query), use(new std::size_t(1)) { }
Query_base *q;
std::size_t *use;
void decr_use()
{ if (--*use == 0) {delete q; delete use; } }
};


Things I feel strange are that it looks like this inline function is about
an operator '<<'. Is it still an overloading? Then, this overloading
function is not a class member function. If that is OK, I can define any
operator overloading function, not a member function for a class?

Thanks,

Ben Bacarisse

unread,
Oct 7, 2015, 3:17:21 PM10/7/15
to
fl <rxj...@gmail.com> writes:
<snip>
> Things I feel strange are that it looks like this inline function is about
> an operator '<<'. Is it still an overloading?

Yes.

> Then, this overloading
> function is not a class member function.

That's right. It's a plain old-fashioned function.

> If that is OK, I can define any
> operator overloading function, not a member function for a class?

Pretty much. Overloading based on argument type is a general mechanism
in C++, and it is often used with plain functions. operator<< is a very
common case.

--
Ben.

K. Frank

unread,
Oct 8, 2015, 6:22:40 PM10/8/15
to
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
0 new messages