I've got a following code written in C++:
#include <iostream>
using namespace std;
class Window;
class Level
{
int level;
int get(Window& w);
public:
Level(void): level(3) {}
void show(Window& w);
};
void Level::show(Window& w)
{
cout << get(w) << endl;
}
class Item
{
static const int item = 8;
};
class Window
{
friend int Level::get(Window& w);
int window;
public:
Window(void): window(2) {}
void show(void);
};
void Window::show(void)
{
cout << "window" << endl;
}
int Level::get(Window& w)
{
return w.window + level;
}
int main()
{
Window wnd;
Level lvl;
wnd.show();
lvl.show(wnd);
cin.get();
return 0;
}
I want to have access to private member of class Window
only accessible by friend function get
, which is also private function of class Level
. When I'm trying to compile I've got an error C2248. Is that possible to make private function as friend of other class?
Must friend declaration names be accessible? quotes the standard wording that "[a] name nominated by a friend declaration shall be accessible in the scope of the class containing the friend declaration", and gives a rationale for the existence of that (somewhat odd-looking) rule.