A nested class is a class which is declared in another enclosing class. A nested class is a member and as such has the same access rights as any other member. The members of an enclosing class have no special access to members of a nested class; the usual access rules shall be obeyed.
For example, program 1 compiles without any error and program 2 fails in compilation.
Program 1
#include<iostream>
using namespace std;
class Enclosing {
private :
int x;
class Nested {
int y;
void NestedFun(Enclosing *e) {
cout<<e->x;
}
};
};
int main()
{
}
|
Program 2
#include<iostream>
using namespace std;
class Enclosing {
int x;
class Nested {
int y;
};
void EnclosingFun(Nested *n) {
cout<<n->y;
}
};
int main()
{
}
|
References:
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1905.pdf
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
04 Jan, 2019
Like Article
Save Article