C++友元运算符重载

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include<stdio.h>

class CTestOperator
{
public:

CTestOperator();
friend CTestOperator operator++(CTestOperator& t,int a);
friend void Print(CTestOperator& t);

private:
int x;
int y;

};

void Print(CTestOperator& t)
{
printf("%d\t%d\n", t.x, t.y);
}

CTestOperator::CTestOperator()
{
this->x = 10;
this->y = 20;
}

CTestOperator operator++(CTestOperator& t,int a)
{
t.x++;
t.y++;
return t;
}

void test()
{
CTestOperator t;
Print(t);
t++;
Print(t);
}

int main()
{
test();
return 0;
}

C++友元运算符重载
https://rogxo.github.io/2021/12/01/2021-12-01-C++友元运算符重载/
作者
Rogxo
发布于
2021年12月1日
许可协议
CC BY-NC-SA 4.0