Is it possible to declare a friend function as static?

前端 未结 3 750
南旧
南旧 2020-12-08 10:23

Here is some C++ example code that compiles and works fine:

class A
{
public:
   A() {/* empty */}

private:
   friend void IncrementValue(A &);
   int v         


        
3条回答
  •  北海茫月
    2020-12-08 10:54

    Sure. Read the second line of the error message carefully: the function was declared extern and later static. So all you have to do is declare it static before the friend declaration:

    class A;
    static void IncrementValue(A&);
    
    class A {
        // class definition, including friend declaration
    };
    
    static void IncrementValue(A&) {
        // code here, of course
    }
    

提交回复
热议问题