因为所有的类模板所做的工作是产生类
所以类模板可以很好地与RTTI一起工作
RTTI提供了一条方便的途径来获得对象所在类的名称
//: C08:ConstructorOrder.cpp
// From "Thinking in C++, Volume 2", by Bruce Eckel & Chuck Allison.
// (c) 1995-2004 MindView, Inc. All Rights Reserved.
// See source code use permissions stated in the file 'License.txt',
// distributed with the code package available at www.MindView.net.
// Order of constructor calls.
#include <iostream>
#include <typeinfo>
using namespace std;
template<int id> class Announce {
public:
Announce() {
cout << typeid(*this).name() << " constructor" << endl;
}
~Announce() {
cout << typeid(*this).name() << " destructor" << endl;
}
};
class X : public Announce<0> {
Announce<1> m1;
Announce<2> m2;
public:
X() { cout << "X::X()" << endl; }
~X() { cout << "X::~X()" << endl; }
};
int main() { X x; getchar();} ///:~
输出
class Announce<0> constructor
class Announce<1> constructor
class Announce<2> constructor
X::X()
这个模板用一个int常量把一个类和其他分区很开
但是也可使用类型参数
在构造函数和析构函数内部
RTTI信息参数打印的类名