multimap<Base> basket; Base base; Derived derive; basket.insert(base); //ok,add copy of base; basket.insert(derive); //ok,but derive sliced down to its base part.
//Base.h #pragma once class Base { public: Base(void); virtual ~Base(void); virtual Base* clone() const; virtual void fine() const; private: int mb; };
//Base.cpp #include "Base.h" #include <iostream> using namespace std; Base::Base(void):mb(12) { } Base::~Base(void) { } Base* Base::clone() const { return new Base(*this); } void Base::fine() const { cout<<"Base fine function"<<endl; }
//Derive.h #pragma once #include "base.h" class Derive : public Base { public: Derive(void); virtual ~Derive(void); virtual Derive* clone() const; virtual void fine() const; private: int md; };
//Derive.cpp #include "Derive.h" #include <iostream> using namespace std; Derive::Derive(void):Base(),md(13) { } Derive::~Derive(void) { } Derive* Derive::clone() const { return new Derive(*this); } void Derive::fine() const { cout<<"Derive fine function"<<endl; }
//Handles.h #pragma once #include "Base.h" #include <iostream> class Handles { public: Handles(void); Handles(const Base&); Handles(const Handles& h); ~Handles(void); const Base* operator->()const; const Base& operator*()const; Handles& operator=(const Handles&); private: Base* p; std::size_t* use; void dec_use() { if(--*use == 0) { delete p; delete use; std::cout<<"delete resource"<<std::endl; } } };
//Handle.cpp #include "Handles.h" Handles::Handles(void):p(NULL),use(new size_t(1)) { } Handles::Handles(const Handles& h):p(h.p),use(h.use) { ++*use; } Handles::Handles(const Base& item):p(item.clone()),use(new std::size_t(1)) { } const Base& Handles::operator*()const { if(p) return *p; else throw std::logic_error("unbounded Handles"); } const Base* Handles::operator->()const { if(p) return p; else throw std::logic_error("unbounded Handles"); } Handles& Handles::operator=(const Handles& h) { ++*h.use; dec_use(); p = h.p; use = h.use; return *this; } Handles::~Handles() { dec_use(); }
//main.cpp #include <iostream> #include "Handles.h" #include "Derive.h" #include <vector> using namespace std; void main() { vector<Handles> mb; Base b; Derive d; mb.push_back(Handles(b)); mb.push_back(Handles(d)); mb[0]->fine(); mb[1]->fine(); system("pause"); }
原文地址:http://blog.csdn.net/ddupd/article/details/38160973