1、友元的声明只能出现在类定义的内部,

2、可以出现在类中的任何地方,

3、友元不是类的成员函数!所以它的声明可以出现在类中任何地方,而不受声明前面的访问控制影响!

 

以上几条可见下例子:

#include <iostream>
using namespace std;

class TestPoint {
private:
	int x;
	int y;
	friend int distanceOne();	//友元的声明可以出现在类内任何地方,它不是类的成员函数!
public:
	friend int distanceTwo();	//友元的声明可以出现在类内任何地方,它不是类的成员函数!
};

int distance()	//这里如果为 friend int distance()则是错误的!声明只能出现在类内部!
{
	return 0;
}

int main(void)
{
	return 0;
}


相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-10-29
  • 2021-08-13
  • 2021-09-11
  • 2021-06-23
  • 2021-11-13
猜你喜欢
  • 2022-02-24
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-04
  • 2021-06-19
  • 2021-06-24
相关资源
相似解决方案