- Modern C++:Efficient and Scalable Application Development
- Richard Grimes Marius Bancila
- 101字
- 2021-06-10 18:28:15
Nested classes
You can define a class within a class. If the nested class is declared as public, then you can create objects in the container class and return them to external code. Typically, however, you will want to declare a class that is used by the class and should be private. The following declares a public nested class:
class outer
{
public:
class inner
{
public:
void f();
};
inner g() { return inner(); }
};
void outer::inner::f()
{
// do something
}
Notice how the name of the nested class is prefixed with the name of the containing class.