⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 typedbin.h

📁 Think in C++ 第二版源码
💻 H
字号:
//: C25:TypedBin.h

// From Thinking in C++, 2nd Edition

// Available at http://www.BruceEckel.com

// (c) Bruce Eckel 1999

// Copyright notice in Copyright.txt

#ifndef TYPEDBIN_H

#define TYPEDBIN_H

#include "Trash.h"

#include "Aluminum.h"

#include "Paper.h"

#include "Glass.h"

#include "Cardboard.h"

#include <vector>



// Template to generate double-dispatching

// trash types by inheriting from originals:

template<class TrashType> 

class DD : public TrashType {

protected:

  DD() : TrashType(0) {}

  friend class TrashPrototypeInit;

public:

  DD(double wt) : TrashType(wt) {}

  bool addToBin(std::vector<TypedBin*>& tb) {

    for(int i = 0; i < tb.size(); i++)

      if(tb[i]->add(this))

        return true;

    return false;

  }

  // Override clone() to create this new type:

  Trash* clone(const Trash::Info& info) {

    return new DD(info.data());

  }

};



// vector<Trash*> that knows how to 

// grab the right type

class TypedBin : public std::vector<Trash*> {

protected:

  bool addIt(Trash* t) {

    push_back(t);

    return true;

  }

public:

  virtual bool add(DD<Aluminum>*) {

    return false;

  }

  virtual bool add(DD<Paper>*) {

    return false;

  }

  virtual bool add(DD<Glass>*) {

    return false;

  }

  virtual bool add(DD<Cardboard>*) {

    return false;

  }

};



// Template to generate specific TypedBins:

template<class TrashType>

class BinOf : public TypedBin {

public:

  // Only overrides add() for this specific type:

  bool add(TrashType* t) { return addIt(t); }

};

#endif // TYPEDBIN_H ///:~

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -