简体   繁体   English

在 class 声明中定义朋友 function

[英]Defining friend function inside class declaration

I have found that friend function can be defined inside class declaration.我发现朋友 function 可以在 class 声明中定义。 I am a little bit confused about what does it mean due to a class declaration does not provide it's body, in general it just class A;由于 class 声明没有提供它的主体,我对它的含义有点困惑,通常它只是class A; . .

Friend functions can be defined (given a function body) inside class declarations.友函数可以在 class 声明中定义(给定一个 function 主体)。 These functions are inline functions, and like member inline functions they behave as though they were defined immediately after all class members have been seen but before the class scope is closed (the end of the class declaration). These functions are inline functions, and like member inline functions they behave as though they were defined immediately after all class members have been seen but before the class scope is closed (the end of the class declaration). Friend functions that are defined inside class declarations are in the scope of the enclosing class.在 class 声明中定义的友元函数位于封闭 class 的 scope 中。

Source: https://docs.microsoft.com/en-us/cpp/cpp/friend-cpp?view=vs-2019资料来源: https://docs.microsoft.com/en-us/cpp/cpp/friend-cpp?view=vs-2019

It means that代表着

class A
{
public:
    friend std::ostream& operator << (std::ostream& os, const A&a)
    {
        return os << a.m1 << a.m2;
    }
};

is equivalent to相当于

class A
{
public:
    friend std::ostream& operator << (std::ostream& os, const A&a);
};

inline std::ostream& operator << (std::ostream& os, const A&a)
{
    return os << a.m1 << a.m2;
}

A class declaration can be at the same time a class definition. class 声明可以同时是 class 定义。

You can not place a friend function definition in a class that is not being defined.您不能将朋友 function 定义放在未定义的 class 中。

Take into account that if such a friend function is not also declared outside the class granting the friendship then it is invisible.考虑到如果这样的朋友 function 也没有在 class 之外声明,那么它是不可见的。 It can be found only by the argument dependent name lookup.它只能通过参数依赖名称查找来找到。

Here is a demonstrative program.这是一个演示程序。

#include <iostream>

class A
{
private:
    int x;

public:
    A( int x ) : x ( x ) {}
    operator int() const { return x; }
    friend void f( const A &a )
    {
        std::cout << "a.x = " << a.x << '\n';
    }
};

void f( int x )
{
    std::cout << "x = " << x << '\n';
}

int main() 
{
    f( A( 10 ) );
    ( f )( A( 20 ) );

    return 0;
}

The program output is程序 output 是

a.x = 10
x = 20

In this call在这次通话中

f( A( 10 ) );

the name of the friend function is found due to the argument dependent lookup.由于参数相关查找,找到了朋友 function 的名称。

In this call在这次通话中

( f )( A( 20 ) );

enclosing the function name in parentheses switches off the argument dependent look up and the non-friend function is called.将 function 名称括在括号中会关闭参数相关查找,并调用非朋友 function。

Or you could use a qualified function name like或者您可以使用合格的 function 名称,例如

::f( A( 20 ) );

In this case the ADL also will not work and the non-friend function will be called.在这种情况下,ADL 也将不起作用,并且将调用非朋友 function。

Also bear in mind that friend function defined in a class is in the lexical scope of the class.还要记住,在 class 中定义的朋友 function 在 ZA2F2ED4F29DC40EBC2CBB4ZC2 的词汇 scope 中。 It means that it can use names of members of the class.这意味着它可以使用 class 成员的名称。 Friend function defined outside a class is not in the lexical scope of the class.在 class 外部定义的朋友 function 不在 ZA2F2ED4F8EBC2CBB4C21A29 的词法 scope 中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM