You can define a class inside another class, but it doesn't make an instance of the nested class aware of an instance of the outer class.
It does, in practice, confer access. But the rules for that have changed at least two times, and I think it's three times. Here's an example:
#include <iostream>
using namespace std;
class Outer
{
private:
int blah_;
class Inner
{
public:
void foo()
{
cout << Outer().blah_ << endl;
}
};
public:
Outer(): blah_( 42 ) {}
void bar() { Inner().foo(); }
};
int main()
{
Outer().bar();
}
This compiles nicely with MinGW g++ 4.4.1, Visual C++ 10.0, and Comeau Online 4.3.10.1. It's as if the nested class is automatically a friend
of the outer class. But again, the rules for that have been in flux, and are perhaps still in flux, I don't know.
Expressing the same without using a nested class definition would make the nested class visible to client code.
But it illustrates what goes on, what the code above means:
#include <iostream>
using namespace std;
class Inner;
class Outer
{
friend class Inner;
private:
int blah_;
public:
Outer();
void bar();
};
class Inner
{
public:
void foo();
};
Outer::Outer(): blah_( 42 ) {}
void Outer::bar() { Inner().foo(); }
void Inner::foo()
{
cout << Outer().blah_ << endl;
}
int main()
{
Outer().bar();
}
By the way, as you can see nesting class definitions has nothing to do with templating. Commonly we just say that such logically unrelated features are "orthogonal", meaning that they generally don't interact.
Cheers & hth.,