理解ATL中的ATLNOTABLE和模板实现不带虚函数表之多态
Wentao Sun
#include <iostream>
using namespace std;
template <typename T>
class B1
{
public:
void sayHi()
{
T* pT = static_cast<T*>(this);
pT->printClassName();
}
void printClassName()
{
cout << "This is B1 " << endl;
}
};
class D1 : public B1<D1>
{
public:
void printClassName()
{
cout << "This is D1" << endl;
}
};
class D2 : public B1<D2>
{
public:
void printClassName()
{
cout << "This is D2" << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
D1 d1;
D2 d2;
d1.sayHi();
d2.sayHi();
return 0;
}
using namespace std;
template <typename T>
class B1
{
public:
void sayHi()
{
T* pT = static_cast<T*>(this);
pT->printClassName();
}
void printClassName()
{
cout << "This is B1 " << endl;
}
};
class D1 : public B1<D1>
{
public:
void printClassName()
{
cout << "This is D1" << endl;
}
};
class D2 : public B1<D2>
{
public:
void printClassName()
{
cout << "This is D2" << endl;
}
};
int _tmain(int argc, _TCHAR* argv[])
{
D1 d1;
D2 d2;
d1.sayHi();
d2.sayHi();
return 0;
}