Although a nested class is defined in the scope of its enclosing class, it is important to understand that there is no connection between the objects of an enclosing class and objects of its nested classe(s). A nested-type object contains only the members defined inside the nested type. Similarly, an object of the enclosing class has only those members that are defined by the enclosing class. It does not contain the data members of any nested classes.
More concretely, the second return
statement in TextQuery::query
return QueryResult(sought, loc->second, file);
uses data members of the TextQuery
object on which query
was run to initialize a QueryResult
object. We have to use these members to construct the QueryResult
object we return because a QueryResult
object does not contain the members of its enclosing class.