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

📄 bicycle.h

📁 Thinking_in_C___2.rar
💻 H
字号:
//: C21:Bicycle.h
// From Thinking in C++, 2nd Edition
// at http://www.BruceEckel.com
// (c) Bruce Eckel 1999
// Copyright notice in Copyright.txt
// Interesting class for use with STL algorithms
#ifndef BICYCLE_H_
#define BICYCLE_H_
#include <vector>
#include <iostream>
#include <typeinfo>

class BicyclePart {
  class LeakChecker {
    int count;
  public:
    LeakChecker() : count(0) {}
    void print() {
      std::cout << count << std::endl; 
    }
    ~LeakChecker() { print(); }
    void operator++(int) { count++; }
    void operator--(int) { count--; }
  };
  static LeakChecker lc;
public:
  BicyclePart() { lc++; }
  virtual BicyclePart* clone() = 0;
  virtual ~BicyclePart() { lc--; }
  friend std::ostream& 
  operator<<(std::ostream& os, BicyclePart* bp) {
    return os << typeid(*bp).name();
  }
  friend class Bicycle;
};

enum BPart {
  Frame, Wheels, Seat, HandleBars, 
  Sprockets, Deraileur,
};

template<BPart id> 
class Part : public BicyclePart {
public:
  BicyclePart* clone() { return new Part<id>; }
};

class Bicycle {
public:
  typedef std::vector<BicyclePart*> VBP;
  Bicycle();
  Bicycle(const Bicycle& old);
  Bicycle& operator=(const Bicycle& old);
  // [Other operators as needed go here:]
  // [...]
  // [...]
  ~Bicycle();
  // So you can change parts on a bike (but be 
  // careful: you must clean up any objects you
  // remove from the bicycle!)
  VBP& bikeParts() { return parts; }
  friend std::ostream& 
  operator<<(std::ostream& os, Bicycle* b);
  static void print(std::vector<Bicycle*>& vb, 
    std::ostream& os = std::cout);
private:
  static int counter;
  int id;
  VBP parts;
  void purge() {
    for(VBP::iterator it = parts.begin();
      it != parts.end(); it++)
      delete *it;
  }
};

class BicycleGenerator {
public:
  Bicycle* operator()();
}; 
#endif // BICYCLE_H_ ///:~

⌨️ 快捷键说明

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