-
Notifications
You must be signed in to change notification settings - Fork 1
/
item12_declare_override.cpp
74 lines (66 loc) · 1.9 KB
/
item12_declare_override.cpp
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
#include <iostream>
#include <vector>
#include <memory>
using namespace std;
using dataType = vector<int>;
class Widget{
public:
Widget(dataType& _data):data(_data){}
dataType& get() &{
cout << "return reference version" << endl;
return data;
}
dataType get() &&{
cout << "invoke move version" << endl;
return std::move(data);
}
private:
dataType data;
};
class Base{
public:
virtual void mf1() const & { cout << "Invoke Base's mf1() function" << endl;}
virtual void mf1() const && { cout << "Rvalue Invoke Base's mf1() function" << endl;}
};
// better with the override keyword.
class Derived0 : public Base{
public:
virtual void mf1() const & override {cout << "Invoke Derived0's mf1() function" << endl;}
virtual void mf1() const && override { cout << "Rvalue Invoke Derived0's mf1() function" << endl;}
};
class Derived1: public Derived0{
public:
virtual void mf1() const & override {cout << "Invoke Derived1's mf1() function" << endl;}
virtual void mf1() const && override { cout << "Rvalue Invoke Derived1's mf1() function" << endl;}
};
Widget makeWidget(){
dataType d{1,2};
Widget w(d);
return w;
}
int main(){
dataType v{1,2,3};
Widget w(v);
auto t0 = w.get();
auto t = std::move((*std::make_unique<Widget>(v))).get();
for(auto _t :t){
cout <<_t << endl;
}
// use factory;
auto _t = makeWidget().get();
// Test the grandpa->father-son derivation.
Derived1 d1;
Base & b1 = d1;
cout << "Invoke mf1 from Base reference to d1" << endl;
b1.mf1();
Derived0 &d0 = d1;
cout << "Invoke mf1 from Derived0 reference to d1" << endl;
d0.mf1();
// I did not figure out why this rvalue can invoke
// lvalue function type.
std::cout << "tmp rvalue's invoke \n";
Derived1{}.mf1();
Derived0{}.mf1();
Base{}.mf1();
return 0;
}