定义在类中的友元函数的访问权限?

这个问题是个有趣的问题。

问题的答案是:类域内定义的友元函数调用时必须具有该类类型的实参。解释如下:

首先类域内定义的友元函数应该如何看待呢?C++标准的规定:

11.4 Friends

A function can be defined in a friend declaration of a class if and only if the class is a non-local class (9.8), the function name is unqualified, and the function has namespace scope. [Example:

class M {
friend void f() { }   // definition of global f, a friend of M,
                      // not the definition of a member function
};
—end example] 
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 (3.4.1).


f不是qualified-id,但函数体本身处于类域构成的名称空间域内。这意味着,不能使用A::f这样的qualified-id,但调用时又必须体现一个qualified的函数体,这如何做到?还是标准的条款:

7.3.1.2 Namespace member definitions

If a friend function is called, its name may be found by the name lookup that considers functions from namespaces and classes associated with the types of the function arguments (3.4.2).

当调用一个友元函数时,名字搜索域也包含与实参关联的那些名称空间和类中的名字,于是,一个如下定义的友元函数可以被调用:

friend void f( A& a ) {}

但是如果f的形参不是A类型的行不行呢?当然行,但必须具备有效的从A到f形参类型的转换,例如:

#include <iostream>

class A
{
public:
    friend void f( int a ){ }

    operator int( ){ return a; }

private:

    int a;
};

int main( void )
{
    A a;
    f( a );
    return 0;
}

除此之外都是非法调用。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值