[英]How to declare friend with static function in Outer class inside the Inner class?
Here is the simple code:这是简单的代码:
class Outer
{
public:
class Inner
{
static friend void Outer::s_foo(Inner*); //<-- How to declare that?
private:
void inner_foo() {}
};
static void s_foo(Inner * inner)
{
inner->inner_foo();
}
};
Is it possible to declare friendship correctly?是否可以正确声明友谊?
This is a case where order is important.这是顺序很重要的情况。 To know
s_foo
exists, the friend
declaration must come after s_foo
要知道
s_foo
存在, friend
元声明必须在s_foo
之后
class Outer
{
public:
class Inner; // forward declaration to satisfy s_foo's argument
static void s_foo(Inner * inner) // moved ahead of friend declaration
{
inner->inner_foo();
}
class Inner
{
friend void Outer::s_foo(Inner*); //No need for static here. Just need the name
private:
void inner_foo() {}
};
};
As for why s_foo
can see inner_foo
even though it is declared later, That's just C++ being friendly.至于为什么
s_foo
声明了也能看到inner_foo
,那只是C++友好。 When resolving the methods C++ considers the whole class.在解析方法 C++ 时会考虑整个 class。 Why it can't do the same for the
friend
declaration, frankly I don't know.为什么它不能对
friend
声明做同样的事情,坦率地说我不知道。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.