Please read the code to know the problem :
#include <iostream>
void fun(int value)
{
//starts local class definition
class test
{
int x;
public:
test(int a) : x(a) {}
void display() const
{
std::cout << "x = " << x << std::endl;
}
};
//end of the definition
test t1(value);
t1.display();
//if we write the statement t1.x=100; here .It will give us an error
//because we can not access the private members from the enclosing function
//now what should I do if I want to access the private members of the test class from fun function
}
int main()
{
fun(5);
}
Should I make the fun function as friend for the local class(test). I was reading a book and there was said that we can achieve this by declaring the enclosing function as a friend. Now my problem is this I don't know how to make the enclosing function as a friend of the local class. Please, someone tell me how can I do so.
Qualified call
friend void ::fun(int);is a way:Demo
Notice than some old version of g++ reject it.