Thinking In C ++ Volume 2

zhaozj2021-02-16  63

Garbage collector

In order to further explain the special usage of RTTI, the following code simulates a garbage collector. Different kinds of "garbage" is placed in a simple container, which is sorted according to their dynamic type.

//: c08: trash.h

// Describing trash.

#ifndef trash_h

#define trash_h

#include

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

The Static value representing the garbage per unit price is defined in the implementation file:

//: 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 () template is looped, displayed and calculated from a container:

//: C08: Recycle.cpp

// {l} trash

// a trash recycler.

#include

#include

#include

#include

#include

#include "trash.h"

#include "../purge.h"

Using namespace std;

// Sums up the value of the trash in a bin:

Template

Void Sumvalue (Container & Bin, Ostream & OS) {

TypeName Container :: item 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 bin;

// Fill Up The 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 (()% 1000) / 10.0);

Break;

Case 2:

bin.push_back (new glass (()% 1000) / 10.0);

Break;

}

// NOTE: BINS HOLD EXACT TYPE OF Object, NOT BASE TYPE:

Vector Glassbin;

Vector paperbin;

Vector alumbin;

Vector :: iterator sORTER = bin.begin ();

// sort the trash:

While (sORTER! = bin.end ()) {

Aluminum * ap = Dynamic_cast (* sORTER);

Paper * pp = Dynamic_cast (* sORTER);

Glass * GP = Dynamic_cast (* 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);

} ///: ~

Garbage is thrown into a simple BIN without category, so the specific type information is lost. However, the subsequent type can be resumed to be recovered to properly sort the garbage, so it uses RTTI.

By contacting the MAP of a pointer and Type_info object, the Type_info object with a TRASH pointer, we can improve this solution. Because a MAP needs to be sorted predicates, we provide one, named Tinfoless, which calls type_info :: before (). When we insert the trash pointer into the map, they will automatically connect with their Type_info keywords. Note that Sumvalue () must be defined differently.

转载请注明原文地址:https://www.9cbs.com/read-22919.html

New Post(0)