简体   繁体   English

如何在内部 class 内声明与 static function 的朋友?

[英]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.

相关问题 我可以将外部类的成员函数声明为内部类的朋友函数吗 - Can I declare a member function of the outer class to be the friend function of in an inner class 成员函数Outer :: f()不是类Outer :: Inner的朋友。 为什么? - The member function Outer::f() is not a friend of class Outer::Inner. Why? 在CPP类中声明一个C函数作为朋友 - declare a C function as friend inside CPP class 如何在 class 内声明一个模板的朋友 function 并在 ZA2F221ED4F8EBC29DCBDC4 外部实现这个朋友 function - How to declare a friend function of a template inside a class and implement this friend function ouside class? 如何向可变参数类声明模板友元函数 - how to declare a template friend function to a variadic class 如何为一个类声明一个朋友函数? - How do I declare a friend function for a class? 如何在外部 class 中调用内部 class 的 function? - How to call a function of inner class in outer class? 如何在以内部类作为参数的命名空间中声明友元函数? - How can I declare a friend function in a namespace that takes an inner class as a parameter? 如果Outer班是我的朋友,那么类Outer :: Inner呢? - If class Outer is my friend, is class Outer::Inner too? 私人内部阶级的朋友功能 - Friend function of a private inner class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM