Normal rules apply for name lookup (§ 7.4.1, p. 283) inside a nested class. Of course, because a nested class is a nested scope, the nested class has additional enclosing class scopes to search. This nesting of scopes explains why we didn’t define line_no
inside the nested version of QueryResult
. Our original QueryResult
class defined this member so that its own members could avoid having to write TextQuery::line_no
. Having nested the definition of our results class inside TextQuery
, we no longer need this typedef
. The nested QueryResult
class can access line_no
without specifying that line_no
is defined in TextQuery
.
As we’ve seen, a nested class is a type member of its enclosing class. Members of the enclosing class can use the name of a nested class the same way it can use any other type member. Because QueryResult
is nested inside TextQuery
, the query
member of TextQuery
can refer to the name QueryResult
directly:
// return type must indicate that QueryResult is now a nested class
TextQuery::QueryResult
TextQuery::query(const string &sought) const
{
// we'll return a pointer to this set if we don't find sought
static shared_ptr<set<line_no>> nodata(new set<line_no>);
// use find and not a subscript to avoid adding words to wm!
auto loc = wm.find(sought);
if (loc == wm.end())
return QueryResult(sought, nodata, file); // not found
else
return QueryResult(sought, loc->second, file);
}
As usual, the return type is not yet in the scope of the class (§ 7.4, p. 282), so we start by noting that our function returns a TextQuery::QueryResult
value. However, inside the body of the function, we can refer to QueryResult
directly, as we do in the return
statements.