首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

友元函数体实现在类内和实现在类外的区别?该如何处理

2012-06-07 
友元函数体实现在类内和实现在类外的区别?C/C++ code#include iostreamclass A{public:friend void f()

友元函数体实现在类内和实现在类外的区别?

C/C++ code
#include <iostream>class A{public:    friend void f() {};};int main(){     f();    system( "pause" );    return 0;}


如果友元函数实现在类体内,main函数体内的f调用在编译的时候报错,error C3767: “f”: 候选函数不可访问。

请问:友元函数体实现在类内和实现在类外的区别,希望具体点的,多谢各位!
   


[解决办法]
去看herb sutter的“be friending templetes”
[解决办法]
这个问题是个有趣的问题。

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

首先类域内定义的友元函数应该如何看待呢?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形参类型的转换,例如:

C/C++ code
#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;}
[解决办法]
谢谢大侠!学习了!
探讨

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

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

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

11.4 Friends

A function can be defined in a friend declaration of a class if and only if the class is a no……

热点排行