CSDN博客

img wkwhl

thinking in c++ 卷2

发表于2004/4/7 14:31:00  514人阅读

垃圾收集器

为进一步阐述RTTI的特殊用法,下面的代码模拟了一个垃圾收集器。不同种类的“垃圾”被放入一个简单的容器,以后依据他们的动态类型排序。

//: C08:Trash.h

// 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}

// 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

//{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);

} ///:~

垃圾被毫无类别的扔进一个简单的bin,因此具体的类型信息就丢失了。但是以后的类型能够信息必须被恢复以正确对垃圾排序,所以就用到了RTTI

通过使用联系一个指针和type_info对象的map,该type_info对象带有一个Trash指针的vector我们能改进这种解决办法。因为一个map需要已排序的谓词,我们提供了一个,名字叫TInfoLess,它调用type_info::before( )。当我们把Trash指针插入到map中,他们就自动和他们的type_info关键字联系起来了。注意这儿sumValue( )必须不同地被定义。

我们修改了sumValue( )来直接调用type_info::name( ),因为type_info对象作为TrashMap::value_type对的成员现在是可用的。这避免了额外的调用typeid来获得正在处理的,在程序的将来版本中必须的Trash类型。

阅读全文
0 0

相关文章推荐

  • Thinking in C++ chapter 2
    ibmyself 2015-03-05 11:00 632
  • thinking in c++ part 2
    Thinking_boy1992 2017-04-25 08:46 79
  • Thinking in C++, 2nd Edition
    Trust_haitao365 2009-01-15 15:45 1
  • Thinking in C++ 运算符重载
    Griselda 2017-06-25 16:42 109
  • Thinking in C++(2nd) pdf
    u011137631 2016-12-21 10:01 6
  • C++学习笔记(Thinking in C++) 二
    libaohan 2011-04-05 23:45 418
  • thinking in c++ v2
    feixue2588 2009-04-02 16:50 43
  • Thinking in C++2nd
    enlonely 2009-11-06 22:53 2
img
取 消
img