Is there any difference if we define friend function inside or outside of class

前端 未结 6 1304
借酒劲吻你
借酒劲吻你 2020-12-10 16:09

What is the difference between defining friend function inside the class or declaring inside and define outside of the class. Also why it is possible to place definition ins

相关标签:
6条回答
  • 2020-12-10 16:14

    There are subtle implications of the following regarding member access.

    C++11 §11.4/5

    A friend function defined in a class is in the (lexical) scope of the class in which it is defined. A friend function defined outside the class is not (3.4.1).

    Still there as of C++17 §14.3/7

    Such a function is implicitly an inline function (10.1.6). A friend function defined in a class is in the (lexical) scope of the class in which it is defined. A friend function defined outside the class is not (6.4.1).

    Consider the condenced example from cppreference [Friend function definition], where f1 finds class static member and f2 finds global variable.

    int i = 3;
    struct X {
        friend void f1(int x) {
            i = x; // finds and modifies X::i
        }
        friend inline void f2(int);
        static const int i = 2;
    };
    
    inline void f2(int x) {
        i = x; // finds and modifies ::i
    }
    

    Of course this can't affect design desicions for the friend function. The main consideration between choices is a difference in name look up as already mentioned in the other answer. Don't forget the inline for f2 to match those of f1 implied by default.

    0 讨论(0)
  • 2020-12-10 16:15

    It is not proper to define a friend function inside a class.

    We use friend function to grant access to private members of the class to a function.

    If we define friend function inside class then in a way it becomes a data member of the class and we know that data member of the own class has access to its private members.

    So if you really want to make use of the friend concept in C++ then declare it outside of the private class.

    0 讨论(0)
  • 2020-12-10 16:28

    As long as the friend function is declared inside the class (it has to be) it doesn't matter where it's defined.

    Also, defining a friend function inside a class implicitly makes the function inline as well.

    Also (from the C++11 spec, §11.3/7):

    A friend function defined in a class is in the (lexical) scope of the class in which it is defined. A friend function defined outside the class is not

    0 讨论(0)
  • 2020-12-10 16:29

    If you define it inside the class it's inline, if it's outside then it isn't.

    0 讨论(0)
  • 2020-12-10 16:30

    A function can be defined in a friend declaration of a class if and only if the class is a non-local class , the function name is unqualified, and the function has namespace scope. Such a function is implicitly inline. A friend function defined in a class is in the (lexical) scope of the class in which it is defined. A friend function defined outside the class is not .

    C++11, [class.friend], ¶6-7

    0 讨论(0)
  • 2020-12-10 16:36

    Friend functions defined inside the class can only be looked up through ADL when called from outside the class. Functions defined outside of the class can be found even without ADL.

    0 讨论(0)
提交回复
热议问题