-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstatic_friend.cpp
More file actions
41 lines (33 loc) · 772 Bytes
/
static_friend.cpp
File metadata and controls
41 lines (33 loc) · 772 Bytes
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
#include <iostream>
struct base {
virtual ~base() = default;
private:
virtual void func(int) = 0;
friend struct Attorney;
};
int foo() {
return 0;
}
struct derived: public base {
~derived() = default;
private:
virtual void func(int) {
std::cout << "derived" << std::endl;
}
};
struct Attorney {
private:
// here & => means call by ref like pointer
// so it triggers polymorphism
// but friendship is not inherited at inheritance
// so if we tried derived it will not allow
// we re just using base & friendship while hiding its actually derived
static void callFunc(base& b, int x) {
return b.func(x);
}
friend int main();
};
int main() {
derived d;
Attorney::callFunc(d, 10);
}