Skip to content

Commit 797f064

Browse files
committed
friend keyword
1 parent 8d88f11 commit 797f064

3 files changed

Lines changed: 142 additions & 0 deletions

File tree

CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ set(APP_SOURCES
9393
"src/core/datatypes/CStruct.cpp"
9494
"src/core/datatypes/CUnion.cpp"
9595
"src/core/datatypes/TypeConVersions.cpp"
96+
## Class
97+
"src/core/datatypes/class/Friend.cpp"
9698
"src/core/datatypes/class/CConstructors.cpp"
9799
"src/core/datatypes/class/CDestructors.cpp"
98100
"src/core/datatypes/class/SallowDeepCopying.cpp"
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
// cppcheck-suppress-file [functionStatic]
2+
#include <iostream>
3+
#include "ExampleRegistry.h"
4+
namespace {
5+
namespace NonMember {
6+
7+
class Accumulator {
8+
private:
9+
int value_{0};
10+
11+
public:
12+
void add(int value) { value_ += value; }
13+
14+
// print out the Accumulator use <<
15+
// S1: Overloading
16+
std::ostream& operator<<(std::ostream& os) const {
17+
os << "S1: " << value_ << "\n";
18+
return os;
19+
}
20+
21+
// S2: Friend
22+
friend std::ostream& operator<<(std::ostream& os, const Accumulator& acc);
23+
};
24+
25+
std::ostream& operator<<(std::ostream& os, const Accumulator& acc) {
26+
os << "S2: " << acc.value_ << "\n";
27+
return os;
28+
}
29+
30+
void run() {
31+
Accumulator acc{};
32+
acc.add(5);
33+
34+
// Use S1:
35+
acc << std::cout; // backward and weird
36+
37+
// Use S2: expect std::cout << acc;
38+
std::cout << acc; // good
39+
}
40+
41+
} // namespace NonMember
42+
43+
namespace Member {
44+
class Accumulator;
45+
class Display {
46+
public:
47+
void f_display(const Accumulator& acc) const;
48+
49+
void display(const Accumulator& acc) const;
50+
};
51+
52+
class Accumulator {
53+
private:
54+
int value_{0};
55+
56+
public:
57+
void add(int value) { value_ += value; }
58+
59+
// Make Display class a friend of Acc
60+
friend void Display::f_display(const Accumulator& acc) const;
61+
};
62+
63+
void Display::f_display(const Accumulator& acc) const{
64+
std::cout << "S4: " << acc.value_ << "\n";
65+
}
66+
67+
void Display::display(const Accumulator& acc) const {
68+
// std::cout << "S4: " << acc.value_ << "\n"; // cannot access
69+
}
70+
71+
void run() {
72+
Accumulator acc{};
73+
acc.add(-99);
74+
Display d{};
75+
d.display(acc);
76+
d.f_display(acc);
77+
}
78+
} // namespace Member
79+
80+
} // namespace
81+
82+
namespace Class {
83+
84+
class Display;
85+
class Accumulator {
86+
private:
87+
int value_{0};
88+
89+
public:
90+
void add(int value) { value_ += value; }
91+
92+
// Make Display class a friend of Acc
93+
friend class Display;
94+
};
95+
96+
class Display {
97+
public:
98+
void display(const Accumulator& acc) {
99+
std::cout << "S3: " << acc.value_ << "\n";
100+
}
101+
};
102+
103+
void run() {
104+
Accumulator acc{};
105+
acc.add(99);
106+
Display d{};
107+
d.display(acc);
108+
}
109+
} // namespace Class
110+
111+
class Friend : public IExample {
112+
std::string group() const override { return "core/class"; };
113+
114+
std::string name() const override { return "Friend"; };
115+
std::string description() const override { return "Friend examples"; };
116+
117+
void execute() override {
118+
NonMember::run();
119+
Member::run();
120+
Class::run();
121+
};
122+
};
123+
124+
REGISTER_EXAMPLE(Friend, "core/class", "Friend");

src/core/datatypes/class/README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
## 1. Friend
2+
### 1.1. Non Member Function
3+
- It serves the same kind of role as the package access specifier in Java.
4+
- `a << b` C++ tries in this order:
5+
```cpp
6+
1 a.operator<<(b)
7+
2 operator<<(a, b) (non-member function)
8+
9+
X OP Y
10+
operator OP (type_of_X, type_of_Y)
11+
```
12+
### 1.2. Member Function
13+
- It give access to only once specific function.
14+
### 1.3. Class
15+
- Friend Class : I trust this class to see my private data
16+
- It give access to all member functions of another class.

0 commit comments

Comments
 (0)