Debian Codegraph @

yo/classtemplates/examples/staticpolymorphism/multiple.cc

raw
1 #include <iostream>
2
3 using namespace std;
4
5 template <class Derived>
6 class Vehicle
7 {
8 public:
9 void mass()
10 {
11 static_cast<Derived *>(this)->vmass();
12 }
13
14 // no need to implement vmass here, as it should be implemented
15 // by derived classes.
16 };
17
18 class Car: public Vehicle<Car>
19 {
20 friend void Vehicle<Car>::mass();
21
22 private:
23 void vmass()
24 {
25 cout << "Car's vmass called\n";
26 }
27 };
28
29 class Truck: public Car, public Vehicle<Truck>
30 {
31 friend void Vehicle<Car>::mass();
32 private:
33 void vmass()
34 {
35 cout << "Car's vmass called\n";
36 }
37 };
38
39 int main()
40 {
41 Truck{}.mass();
42 }