If i correctly understand your question, it is about whether the class defining a subobject (here Address
) should be declared at same level as the containing class (here Student
) in inside the containing class.
The main difference will be the access to private or protected members of the enclosing class from the subobject. A nested class being a member of its enclosing class will have access to all private members (ref: 11.7 Nested classes [class.access.nest] in n4296 draft for C++ 11).
Here is an example where a nested class access to private members of its enclosing class:
class Person {
public:
class Address {
public:
std::string street;
std::string town;
std::string state;
Address(std::string street, std::string town, std::string state)
:street(street), town(town), state(state) {}
void print(std::ostream&, const Person& p) const;
};
Person(std::string firstname, std::string lastname, Address address);
private:
std::string firstname; // private member
std::string lastname;
Address address;
// other members ...
};
Person::Person(std::string firstname, std::string lastname, Address address)
:firstname(firstname), lastname(lastname), address(address) {};
void Person::Address::print(std::ostream& out, const Person& p) const {
// access to private members of Person class
out << p.firstname << " " << p.lastname << std::endl;
out << street << std::endl;
out << town << std::endl;
out << state << std::endl;
}