使用RTTI能从一个匿名基类的多态指针上发现类型信息
初学者容易误用
因为在学会使用虚函数 进行多态调用方法前
使用RTTI很有效
垃圾再生器
不同种类的 "垃圾"被插入一个容器
根据它们的动态类型进行分类
//: C08:Trash.h
// 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.
// Describing trash.
#ifndef TRASH_H
#define TRASH_H
#include <iostream>
class Trash {
float _weight;
public:
Trash(float wt) : _weight(wt) {}
virtual float value() const = 0;
float weight() const { return _weight; }
virtual ~Trash() {
std::cout << "~Trash()" << std::endl;
}
};
class Aluminum : public Trash {
static float val;
public:
Aluminum(float wt) : Trash(wt) {}
float value() const { return val; }
static void value(float newval) {
val = newval;
}
};
class Paper : public Trash {
static float val;
public:
Paper(float wt) : Trash(wt) {}
float value() const { return val; }
static void value(float newval) {
val = newval;
}
};
class Glass : public Trash {
static float val;
public:
Glass(float wt) : Trash(wt) {}
float value() const { return val; }
static void value(float newval) {
val = newval;
}
};
#endif // TRASH_H ///:~
用来表示垃圾类型的单价的static值定义在实现文件中
//: C08:Trash.cpp {O}
// 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.
// A Trash Recycler.
#include "Trash.h"
float Aluminum::val = 1.67;
float Paper::val = 0.10;
float Glass::val = 0.23;
///:~
sumValue()模板从头到尾对一个容器进行迭代
显示并计算结果
//: C08:Recycle.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.
//{L} Trash
// A Trash Recycler.
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <typeinfo>
#include <vector>
#include "Trash.h"
#include "../purge.h"
using namespace std;
// Sums up the value of the Trash in a bin:
template<class Container>
void sumValue(Container& bin, ostream& os) {
typename Container::iterator tally = bin.begin();
float val = 0;
while(tally != bin.end()) {
val += (*tally)->weight() * (*tally)->value();
os << "weight of " << typeid(**tally).name()
<< " = " << (*tally)->weight() << endl;
++tally;
}
os << "Total value = " << val << endl;
}
int main() {
srand(time(0)); // Seed the random number generator
vector<Trash*> bin;
// Fill up the Trash bin:
for(int i = 0; i < 30; i++)
switch(rand() % 3) {
case 0 :
bin.push_back(new Aluminum((rand() % 1000)/10.0));
break;
case 1 :
bin.push_back(new Paper((rand() % 1000)/10.0));
break;
case 2 :
bin.push_back(new Glass((rand() % 1000)/10.0));
break;
}
// Note: bins hold exact type of object, not base type:
vector<Glass*> glassBin;
vector<Paper*> paperBin;
vector<Aluminum*> alumBin;
vector<Trash*>::iterator sorter = bin.begin();
// Sort the Trash:
while(sorter != bin.end()) {
Aluminum* ap = dynamic_cast<Aluminum*>(*sorter);
Paper* pp = dynamic_cast<Paper*>(*sorter);
Glass* gp = dynamic_cast<Glass*>(*sorter);
if(ap) alumBin.push_back(ap);
else if(pp) paperBin.push_back(pp);
else if(gp) glassBin.push_back(gp);
++sorter;
}
sumValue(alumBin, cout);
sumValue(paperBin, cout);
sumValue(glassBin, cout);
sumValue(bin, cout);
purge(bin);
getchar();
} ///:~
输出
weight of class Aluminum = 82.9
weight of class Aluminum = 84.6
weight of class Aluminum = 13.5
weight of class Aluminum = 50.9
weight of class Aluminum = 79.1
weight of class Aluminum = 70.1
weight of class Aluminum = 13
weight of class Aluminum = 93.4
weight of class Aluminum = 76.1
Total value = 941.212
weight of class Paper = 74.2
weight of class Paper = 34.2
weight of class Paper = 19.7
weight of class Paper = 94.7
weight of class Paper = 58.8
weight of class Paper = 51.1
weight of class Paper = 64.8
weight of class Paper = 60.7
weight of class Paper = 9.6
weight of class Paper = 59.5
weight of class Paper = 45.7
weight of class Paper = 23
weight of class Paper = 87.1
weight of class Paper = 44.6
weight of class Paper = 77.3
Total value = 80.5
weight of class Glass = 87.2
weight of class Glass = 79.5
weight of class Glass = 51
weight of class Glass = 52.7
weight of class Glass = 89.4
weight of class Glass = 67.9
Total value = 98.371
weight of class Aluminum = 82.9
weight of class Glass = 87.2
weight of class Glass = 79.5
weight of class Paper = 74.2
weight of class Paper = 34.2
weight of class Aluminum = 84.6
weight of class Paper = 19.7
weight of class Aluminum = 13.5
weight of class Paper = 94.7
weight of class Paper = 58.8
weight of class Paper = 51.1
weight of class Paper = 64.8
weight of class Aluminum = 50.9
weight of class Paper = 60.7
weight of class Aluminum = 79.1
weight of class Aluminum = 70.1
weight of class Paper = 9.6
weight of class Aluminum = 13
weight of class Paper = 59.5
weight of class Glass = 51
weight of class Glass = 52.7
weight of class Paper = 45.7
weight of class Aluminum = 93.4
weight of class Glass = 89.4
weight of class Paper = 23
weight of class Aluminum = 76.1
weight of class Glass = 67.9
weight of class Paper = 87.1
weight of class Paper = 44.6
weight of class Paper = 77.3
Total value = 1120.08
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
因为垃圾被不加分类地投入一个容器中
垃圾的所有具体类型信息就丢失了
但是 稍后适当地对废料进行分类
具体类型信息必须恢复 将用到RTTI
对Trash指针插入映像中的时候
指针与type_info关键字自动关联
对sumValue()进行不同的定义
//: C08:Recycle2.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.
//{L} Trash
// Recyling with a map.
#include <cstdlib>
#include <ctime>
#include <iostream>
#include <map>
#include <typeinfo>
#include <utility>
#include <vector>
#include "Trash.h"
#include "../purge.h"
using namespace std;
// Comparator for type_info pointers
struct TInfoLess {
bool operator()(const type_info* t1, const type_info* t2)
const { return t1->before(*t2); }
};
typedef map<const type_info*, vector<Trash*>, TInfoLess>
TrashMap;
// Sums up the value of the Trash in a bin:
void sumValue(const TrashMap::value_type& p, ostream& os) {
vector<Trash*>::const_iterator tally = p.second.begin();
float val = 0;
while(tally != p.second.end()) {
val += (*tally)->weight() * (*tally)->value();
os << "weight of "
<< p.first->name() // type_info::name()
<< " = " << (*tally)->weight() << endl;
++tally;
}
os << "Total value = " << val << endl;
}
int main() {
srand(time(0)); // Seed the random number generator
TrashMap bin;
// Fill up the Trash bin:
for(int i = 0; i < 30; i++) {
Trash* tp;
switch(rand() % 3) {
case 0 :
tp = new Aluminum((rand() % 1000)/10.0);
break;
case 1 :
tp = new Paper((rand() % 1000)/10.0);
break;
case 2 :
tp = new Glass((rand() % 1000)/10.0);
break;
}
bin[&typeid(*tp)].push_back(tp);
}
// Print sorted results
for(TrashMap::iterator p = bin.begin();
p != bin.end(); ++p) {
sumValue(*p, cout);
purge(p->second);
}
getchar();
} ///:~
输出
weight of class Aluminum = 15.6
weight of class Aluminum = 22.5
weight of class Aluminum = 36.1
weight of class Aluminum = 69.6
weight of class Aluminum = 93.4
weight of class Aluminum = 74.4
Total value = 520.372
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
weight of class Glass = 57.3
weight of class Glass = 98.1
weight of class Glass = 31.7
weight of class Glass = 53.4
weight of class Glass = 32.9
weight of class Glass = 47.9
weight of class Glass = 10.7
weight of class Glass = 76.5
weight of class Glass = 92.8
Total value = 115.299
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
weight of class Paper = 19.9
weight of class Paper = 63.9
weight of class Paper = 42.3
weight of class Paper = 43.3
weight of class Paper = 92.1
weight of class Paper = 1.3
weight of class Paper = 76.7
weight of class Paper = 4.8
weight of class Paper = 43.8
weight of class Paper = 57.9
weight of class Paper = 59.7
weight of class Paper = 23.8
weight of class Paper = 48
weight of class Paper = 42.2
weight of class Paper = 33.8
Total value = 65.35
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
~Trash()
为了调用type_info::name()
修改了sumValue()
作为TrashMap::value_type对的第1个成员
type_info对象现在是可获得的