Friend 函式 (Friend Function)

Friend 函式 (Friend Function)

Friend 函式 教學與筆記。

說明

有時候我們希望私有成員給某些外部函式來存取,這時可以設定類別的 friend 函式,只有 friend 函式可以直接存取該類別的私有成員。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

class Point {
private:
int m_x;
int m_y;
public:
Point(int x, int y) {
m_x = x;
m_y = y;
}
friend void showPoint(Point &pt); // 給其他函式存取私有成員
};

void showPoint(Point &pt) {
cout << "(" << pt.m_x << "," << pt.m_y << ")" << endl;
}

int main() {
Point myPoint(3, 4);
showPoint(myPoint);

return 0;
}
Author

Meow Lucian

Posted on

2019-06-29

Updated on

2022-07-04

Licensed under

Comments