我是C++的新手,当我试图学习朋友函数时,我从Cppreference的朋友描述中看到:
2)(仅在非本地类定义中允许)定义非成员函数,并使其同时成为此类的朋友.这种非成员函数始终是内联的.
class X {
int a;
friend void friend_set(X& p, int i) {
p.a = i; // this is a non-member function
}
public:
void member_set(int i) {
a = i; // this is a member function
}
};
Run Code Online (Sandbox Code Playgroud)
这是否意味着所有朋友的功能必须始终是内联的?换句话说,朋友的功能必须完全在课堂内定义吗?
但是,我还发现了一个实例,其中友元函数在Cplusplus的类外定义
// friend functions
#include <iostream>
using namespace std;
class Rectangle {
int width, height;
public:
Rectangle() {}
Rectangle (int x, int y) : width(x), height(y) {}
int area() {return width …Run Code Online (Sandbox Code Playgroud)