Can a nested class inherit from base class?

Say I have

1
2
3
4
5
6
7
8
9
class base
{
public:
 int i;

 class nested
 {
 };
};


If at some point in my code, I assign 45 to i. Is there a way for my nested class to have access to i? As of now, whenever I create a function that needs both the nested class and the variable i, I am including both the base and nested class in the function. Is there a way I could include only the nested class and still have acces to i?
Last edited on
Can a nested class inherit from base class?

Yes, if you define the nested class outside the enclosing class.
1
2
3
4
5
6
7
8
9
10
11
class base
{
public:
	int i;
	class nested;
};

class base::nested : public base
{
	
};


Is there a way for my nested class to have access to i?

There is nothing special about a nested class. You will have to pass the variables to it like you have to do with any other class.
Last edited on
Thanks.
Peter87
Is there a way for my nested class to have access to i?
There is nothing special about a nested class. You will have to pass the variables to it like you have to do with any other class.


It is incorrect statement. A nested class has no access to non-static data members of the enclosing class.
I didn't say it had.
The author asked: "Is there a way for my nested class to have access to i?" where i is a non-static data member of the enclosing class. And you answered: "There is nothing special about a nested class."

It is incorrect statement. And I pointed out this.
Well, I guess you can argue that there is something special about nested classes. What I meant was that classes can't access non-static members of another class without having an instance of the other class. The same rules apply to nested classes so that was why I said there was nothing special about them.
Topic archived. No new replies allowed.