小编Mol*_*y P的帖子

非成员朋友功能始终是内联的

我是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)

c++ inline friend

9
推荐指数
1
解决办法
264
查看次数

标签 统计

c++ ×1

friend ×1

inline ×1