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