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?
If I read the standard right, this looks like a compiler bug (unusual I know).
11.3/5:
When a friend declaration refers to an overloaded name or operator, only the function specified by the parameter types becomes a friend. A member function of a class X can be a friend of a class Y.
Note that it doesn't say "public member function", just "member function". To me this implies that the privacy of the friendship-receiving function shouldn't be relevant to the granting of friendship.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With