#include <iostream>
using namespace std;
class VirObj {
public:
virtual void what() const {
cout << "Obj !!!" << endl;
}
};
class VirAnimal : public VirObj {
public:
void what() const override {
cout << "Animal !!!" << endl;
}
};
template<typename T>
class Base {
public:
void what() const {
((T *)this)->sayWhat();
}
};
class BaseAnimal : public Base<BaseAnimal> {
public:
void sayWhat() const {
cout << "BaseAnimal !!!" << endl;
}
};
template<typename T>
void virtualWhat(const T &t) {
t.what();
}
template<typename T>
void noVirtualWhat(const Base<T> &t) {
t.what();
}
int main() {
BaseAnimal ba;
noVirtualWhat(ba);
VirObj *vo = new VirAnimal;
virtualWhat(*vo);
}
|