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

Is the example wrong (or not accurate) on set type in declaration?

27 views
Skip to first unread message

fl

unread,
Oct 6, 2015, 5:59:44 PM10/6/15
to
Hi,

I am learning C++ through book 'C++ Primer'. When I read an example project,
I find the set type used are different in declaration and definition,
through it can build well through.


Here is the declaration of class function 'eval':

class AndQuery: public BinaryQuery {
friend Query operator&(const Query&, const Query&);
AndQuery(Query left, Query right):
BinaryQuery(left, right, "&") { }

// concrete class: AndQuery inherits display and defines remaining pure virtual
std::set<line_no> eval(const TextQuery&) const;
};


Here is the definition of function 'eval'. I see the return type is
'set<TextQuery::line_no>'.


set<TextQuery::line_no>
AndQuery::eval(const TextQuery& file) const
{
// virtual calls through the Query handle to get result sets for the operands
set<line_no> left = lhs.eval(file),
right = rhs.eval(file);

set<line_no> ret_lines; // destination to hold results

return ret_lines;
}


On class TextQuery, 'line_no' is defined as:

class TextQuery {
public:
// typedef to make declarations easier
typedef std::string::size_type str_size;
typedef std::vector<std::string>::size_type line_no;


It has a 'vector<std::string>', which is the same as 'std::set<line_no> '?
Is the writing OK?

Thanks,

Paavo Helde

unread,
Oct 7, 2015, 1:13:14 AM10/7/15
to
fl <rxj...@gmail.com> wrote in
news:fb94dd59-b418-40c9...@googlegroups.com:

> Hi,
>
> I am learning C++ through book 'C++ Primer'. When I read an example
> project, I find the set type used are different in declaration and
> definition, through it can build well through.
>
>
> Here is the declaration of class function 'eval':

You mean "member function".

>
> class AndQuery: public BinaryQuery {
> friend Query operator&(const Query&, const Query&);
> AndQuery(Query left, Query right):
> BinaryQuery(left, right, "&") { }
>
> // concrete class: AndQuery inherits display and defines remaining
> pure virtual std::set<line_no> eval(const TextQuery&) const;

There is no 'pure' keyword in C++ AFAIK.

Anyway, the return type of eval is std::set<line_no>. The type line_no is
not defined here, it probably comes from a base class (TextQuery?)

>
>
> Here is the definition of function 'eval'. I see the return type is
> 'set<TextQuery::line_no>'.
>
>
> set<TextQuery::line_no>
> AndQuery::eval(const TextQuery& file) const
> {

Here, the type is set<TextQuery::line_no>. Assuming that there is a using
directive or declaration so that 'set' resolves to 'std::set' and that
TextQuery is a base class of AndQuery so that TextQuery::line_no is the
same type than line_no inside AndQuery definition, these types are the
same. What's the problem?

BTW, one needs to explicitly write TextQuery::line_no here because the
return type is textually placed "outside" of the member function
definition body and thus "cannot see" the names inside the class or
inherited classes without qualification. This is a quirk of C++ syntax.

>
> class TextQuery {
> public:
> // typedef to make declarations easier
> typedef std::string::size_type str_size;
> typedef std::vector<std::string>::size_type line_no;
>
>
> It has a 'vector<std::string>', which is the same as
> 'std::set<line_no> '? Is the writing OK?

No, std::vector is not the same as std::set, if that's what you ask. But
line_no is just defined as a synonym of the type 'size_type' from vector,
so there is actually no vector around.

The vector<T>::size_type type is just an unsigned integer type, typically
32- or 64-bit. So the std::set involved here is just a set of integers.

hth
Paavo

0 new messages