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

📄 chapter4-22.cpp

📁 C++STL程序员开发指南
💻 CPP
字号:
//文件名:CHAPTER4-22.cpp
#include "stl.h"
#include <string.h>
#include <iostream>
#include <list>
using namespace std;
// abstract base class
class Base 
{
public:
    const char* type_name() const { return typename_; }
    virtual Base* clone() const = 0;
    virtual void identify(ostream& os) const = 0;
    virtual ~Base();
public:
    static int count;
protected:
    Base(const char* type_name);
    Base(const Base& base);
private:
    char* typename_;
};
Base::Base(const char* type_name) 
{   const char* tname = (type_name) ? type_name : "unknown";
    strcpy(typename_ = new char[strlen(tname) + 1], tname);
    ++count;
}
Base::Base(const Base& base) 
{   strcpy( typename_ = new char[strlen(base.typename_) + 1], base.typename_ );
    ++count;
}
Base::~Base() 
{   delete[] typename_;
    --count;
}
// First derived class. 
class Derived1 : public Base 
{public:
    Derived1(int data) : Base("derived1"), data_(data) { }
    Derived1(const Derived1& d) : Base("derived1"), data_(d.data()) { }
    virtual ~Derived1()        { }
    virtual Base* clone() const { return new Derived1(*this); }
    virtual void identify(ostream& os) const;
    int data() const { return data_; }
private:
    int data_;
};
void Derived1::identify(ostream& os) const { os << "(" << type_name() << " " << data() << ")"; }
// Second derived class. 
class Derived2 : public Base 
{public:
    Derived2(int data) : Base("derived2"), data_(data) { }
    Derived2(const Derived2& d) : Base("derived2"), data_(d.data()) { }
    virtual ~Derived2()        { }
    virtual Base* clone() const { return new Derived2(*this); }
    virtual void identify(ostream& os) const;
    int data() const { return data_; }
private:
    int data_;
};
void Derived2::identify(ostream& os) const { os << "(" << type_name() << " " << data() << ")"; }
class BaseWrapper 
{public:
    BaseWrapper(Base* base_ptr = 0) : base_ptr_(base_ptr) { }
    BaseWrapper(const BaseWrapper& bw) { base_ptr_ = bw() ? bw()->clone() : 0; }
    ~BaseWrapper() { delete base_ptr_; }
    const Base* operator()()  const { return base_ptr_; }
    Base* operator()()  { return base_ptr_; }
    BaseWrapper& operator= (const BaseWrapper& bw){ delete base_ptr_; base_ptr_ = bw()->clone(); }
private:
    Base* base_ptr_;
};
bool operator== (const BaseWrapper& bw1, const BaseWrapper& w2) { return false; }
bool operator< (const BaseWrapper& bw1, const BaseWrapper& w2) { return false; }
// define static members.
int Base::count = 0;
int main(int, char*[]) 
{
    list<BaseWrapper> list1;
    list1.push_back(BaseWrapper(new Derived1(101)));
    list1.push_back(BaseWrapper(new Derived2(201)));
    list1.push_back(BaseWrapper(new Derived2(202)));
    list1.push_back(BaseWrapper(new Derived1(102)));
    list1.push_back(BaseWrapper(new Derived2(203)));
    list<BaseWrapper>::const_iterator it = list1.begin();
    for(; it != list1.end(); ++it) { const BaseWrapper& bw = *it; bw()->identify(cerr); cerr << " ";}
    cerr << endl << endl;
    return 0;
}

⌨️ 快捷键说明

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