| 1 |
#include <iostream> |
| 2 |
#include <string> |
| 3 |
|
| 4 |
using namespace std; |
| 5 |
|
| 6 |
class VehicleBase |
| 7 |
{ |
| 8 |
// original Vehicle members, not implementing the |
| 9 |
// redefinable interface |
| 10 |
|
| 11 |
template <class Derived> |
| 12 |
friend class Vehicle; |
| 13 |
|
| 14 |
size_t d_massFactor = 1; |
| 15 |
}; |
| 16 |
|
| 17 |
class CarBase: public virtual VehicleBase |
| 18 |
{ |
| 19 |
// original Car members, not implementing the |
| 20 |
// redefinable interface |
| 21 |
|
| 22 |
int x = 1000; |
| 23 |
friend class Car; |
| 24 |
}; |
| 25 |
|
| 26 |
class TruckBase: public CarBase |
| 27 |
{ |
| 28 |
// original Truck members, not implementing the |
| 29 |
// redefinable interface |
| 30 |
|
| 31 |
friend class Truck; |
| 32 |
}; |
| 33 |
|
| 34 |
template <class Derived> |
| 35 |
class Vehicle: public virtual VehicleBase |
| 36 |
{ |
| 37 |
public: |
| 38 |
int mass() const |
| 39 |
{ |
| 40 |
return d_massFactor * |
| 41 |
static_cast<Derived const *>(this)->vmass(); |
| 42 |
} |
| 43 |
}; |
| 44 |
|
| 45 |
class Car: public CarBase, public Vehicle<Car> |
| 46 |
{ |
| 47 |
friend class Vehicle<Car>; |
| 48 |
|
| 49 |
int vmass() const |
| 50 |
{ |
| 51 |
return x; |
| 52 |
} |
| 53 |
}; |
| 54 |
|
| 55 |
class Truck: public TruckBase, public Vehicle<Truck> |
| 56 |
{ |
| 57 |
friend class Vehicle<Truck>; |
| 58 |
|
| 59 |
private: |
| 60 |
int vmass() const |
| 61 |
{ |
| 62 |
return 15000; |
| 63 |
} |
| 64 |
}; |