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

hi5000 2012-06-06 01:31:27

#include <iostream>

class A
{
public:
friend void f() {};
};

int main()
{
f();

system( "pause" );
return 0;
}


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

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

...全文
1724 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
hi5000 2012-06-07
  • 打赏
  • 举报
回复
感谢supermegaboy!
tongzhipeng5699 2012-06-06
  • 打赏
  • 举报
回复
谢谢大侠!学习了!
[Quote=引用 9 楼 的回复:]

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

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

首先类域内定义的友元函数应该如何看待呢?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……
[/Quote]
taodm 2012-06-06
  • 打赏
  • 举报
回复
我记得是herb sutter的文章,或者是《modern c++ design》讲的这个问题。
飞天御剑流 2012-06-06
  • 打赏
  • 举报
回复
这个问题是个有趣的问题。

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

首先类域内定义的友元函数应该如何看待呢?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;
}


除此之外都是非法调用。

依希记得C++ primer第三版谈过这个问题,但身边没有这书,有兴趣的朋友可以自行翻阅。
taodm 2012-06-06
  • 打赏
  • 举报
回复
去看herb sutter的“be friending templetes”
pathuang68 2012-06-06
  • 打赏
  • 举报
回复
友元函数不是成员函数,没有this指针。
pathuang68 2012-06-06
  • 打赏
  • 举报
回复
1. 友元函数必须在类内容声明,这一点应该没有疑问。
2. 一般情况下,友元函数是在类的外部定义的,但在类的内部定义也没有问题,所以说内外皆可。
大尾巴猫 2012-06-06
  • 打赏
  • 举报
回复

#include <iostream>

class A
{
public:
friend void f(const A & a) {}
};

int main()
{
A a;
f(a);

system( "pause" );
return 0;
}

qq120848369 2012-06-06
  • 打赏
  • 举报
回复
看书吧,友元和类成员函数是两码事.
tongzhipeng5699 2012-06-06
  • 打赏
  • 举报
回复 1
印象中觉得好像用友元定义在类里面的,所以以为楼上两位牛人莫非弄错了,结果一百度,还真的是里面声明,外面定义,然后自己写了一下,发现确实要类外定义,但是我找出以前的代码,再写了下面一个代码,我凌乱了...
求解释!..........

#include <iostream>
using namespace std;

// void f(void) {
//
// cout << "in global" << endl;

class A {
private:
int m_a;
public:
A(int n):m_a(n){}
friend ostream& operator<<(ostream& os,const A& a) {
return os<<a.m_a;
}
friend void f(const A& a) {
cout << "in class" << endl;
}
friend void f(void) {
cout << "in class void" << endl;
}
};


int main(void) {

A a(3);
cout << a << endl;
f();//编译错误
f(a);//正常运行,为什么啊?,顺便说一下,VC里面f()也能通过编译.
return 0;
}

[Quote=引用 2 楼 的回复:]

引用 1 楼 的回复:

friend function本质上来说还是global的,不是class scope的,所以只能在类里面声明,在类外面定义。

+++
[/Quote]
W170532934 2012-06-06
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]

friend function本质上来说还是global的,不是class scope的,所以只能在类里面声明,在类外面定义。
[/Quote]
+++
ForestDB 2012-06-06
  • 打赏
  • 举报
回复
friend function本质上来说还是global的,不是class scope的,所以只能在类里面声明,在类外面定义。

65,183

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧