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

How does the friend operator of a class to manage a base class constructor?

26 views
Skip to first unread message

fl

unread,
Oct 31, 2015, 12:52:55 PM10/31/15
to
Hi,

As I remember that the friendship of a class is not inherited, in the
following code, I don't understand the comment:
// these operators need access to the Query_base* constructor

I do see Query constructor:
Query(const Query &c)

but I do not see Query_base constructor.

BTW, this code snippet is from a C++ book.
Could you tell me how to understand the above comment?
Thanks,

............
class Query_base {
friend class Query;
protected:
typedef TextQuery::line_no line_no;
virtual ~Query_base() { }
private:
// eval returns the |set| of lines that this Query matches
virtual std::set<line_no>
eval(const TextQuery&) const = 0;
// display prints the query
virtual std::ostream&
display(std::ostream& = std::cout) const = 0;
};


// handle class to manage the Query_base inheritance hierarchy
class Query {
// these operators need access to the Query_base* constructor
friend Query operator~(const Query &);
friend Query operator|(const Query&, const Query&);
friend Query operator&(const Query&, const 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); }

Öö Tiib

unread,
Oct 31, 2015, 4:43:43 PM10/31/15
to
On Saturday, 31 October 2015 18:52:55 UTC+2, fl wrote:
> Hi,
>
> As I remember that the friendship of a class is not inherited, in the
> following code, I don't understand the comment:
> // these operators need access to the Query_base* constructor
>
> I do see Query constructor:
> Query(const Query &c)
>
> but I do not see Query_base constructor.

I have feeling that it can be typo in comment. Comment tries
to explain why these operators are made 'friend' of 'Query' but the
given reason is confusing. Most easy to find out why these actually
are made friends is perhaps to make these not friends (remove
friend declarations from class) and try to compile.

>
> BTW, this code snippet is from a C++ book.

C++ is quite infamous for hundreds of books about it that have low
quality and may mislead more than help.

fl

unread,
Oct 31, 2015, 4:53:18 PM10/31/15
to
Thanks.
Yes, I have tried to comment out 'friend' keyword. It cannot pass compiling.
In class Query, there is a private member q, which points to Query_base:

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




I check below a derived class. There is a constructor:
BinaryQuery(Query left, Query right, std::string op)

Is it a derived class constructor?

There is operator (|: or, &: and etc.) on it.
The original code may not comment accurately.


........
class BinaryQuery: public Query_base {
protected:
BinaryQuery(Query left, Query right, std::string op):
lhs(left), rhs(right), oper(op) { }

// abstract class: BinaryQuery doesn't define eval
std::ostream& display(std::ostream &os) const
{ return os << "(" << lhs << " " << oper << " "
<< rhs << ")"; }

const Query lhs, rhs; // right- and left-hand operands
const std::string oper; // name of the operator
};

Öö Tiib

unread,
Oct 31, 2015, 5:09:53 PM10/31/15
to
It may be that the comment that you talk about wanted to say something
like: "the operators need access to Query::Query(Query_base*) that is
private and so are made friend.".

0 new messages