5,530
社区成员




template<typename T>
struct world{};
class abstract_animal
{
public:
virtual ~abstract_animal(){}
virtual void eat() = 0;
};
//----------------------------------------------
template<>
class world<abstract_animal>
{
public:
typedef abstract_animal concept_type;
void insert(abstract_animal* aa)
{
objects_.push_back(aa);
}
void erase(abstract_animal* aa)
{
objects_.erase(std::find(objects_.begin(), objects_.end(), aa));
}
void eat() const
{
std::for_each(objects_.begin(), objects_.end(), *this);
}
void operator()(concept_type* eatable) const
{
eatable->eat();
}
private:
static std::vector<concept_type*> objects_;
};
template<> std::vector<abstract_animal*> world<abstract_animal>::objects_;
class animal
:public abstract_animal
{
public:
typedef abstract_animal concept_type;
animal()
{
world<concept_type> w;
w.insert(this);
}
~animal()
{
world<concept_type> w;
w.erase(this);
}
};
//--------------------------
class abstract_flyable: public abstract_animal
{
public:
virtual void fly() =0;
};
template<>
class world<abstract_flyable>: public world<abstract_animal>
{
public:
typedef world<abstract_animal> base_type;
typedef abstract_flyable concept_type;
void insert(concept_type* aa)
{
base_type::insert(aa);
objects_.push_back(aa);
}
void erase(abstract_animal* aa)
{
objects_.erase(std::find(objects_.begin(), objects_.end(), aa));
base_type::erase(aa);
}
void fly() const
{
std::for_each(objects_.begin(), objects_.end(), *this);
}
void operator()(concept_type* object) const
{
object->fly();
}
private:
static std::vector<concept_type*> objects_;
};
template<> std::vector<abstract_flyable*> world<abstract_flyable>::objects_;
class flyable: public abstract_flyable
{
public:
typedef abstract_flyable concept_type;
flyable()
{
world<concept_type> w;
w.insert(this);
}
~flyable()
{
world<concept_type> w;
w.erase(this);
}
};
//-------------------------
class cat: public animal
{
public:
void eat()
{
std::cout<<"Cat("<<this<<") is eating very happy"<<std::endl;
}
};
class dog: public animal
{
public:
void eat()
{
std::cout<<"Dog("<<this<<") is eating very happy"<<std::endl;
}
};
class bird: public flyable
{
public:
void eat()
{
std::cout<<"Bird("<<this<<") is eating worms"<<std::endl;
}
void fly()
{
std::cout<<"Bird("<<this<<") is flying so happy"<<std::endl;
}
};
int main()
{
cat c1;
cat c2;
dog d;
bird b1;
bird b2;
std::cout<<"Now, eatable begin to eating"<<std::endl;
world<abstract_animal> w_animal;
w_animal.eat();
std::cout<<"Now, flyable begin to eating"<<std::endl;
world<abstract_flyable> w_flyable;
w_flyable.fly();
}
class IRun
{
public:
virtual void run() = 0;
};
class DogRun : public IRun
{
public :
void run() { cout <<"dog run" <<endl; }
};
class CatRun : public IRun
{
public :
void run() { cout <<"cat run" <<endl; }
};
class IEat
{
public:
virtual eat() = 0;
};
class DogEat : public IEat
{
public :
void eat() { cout <<"dog Eat" <<endl; }
};
class CatEat : public IEat
{
public :
void eat() { cout <<"cat Eat" <<endl; }
};
class FishEat : public iEat
{
public :
void eat() { cout <<"fish Eat" <<endl; }
};
class Animal
{
public :
Animal() { m_pRun = m_pEat = NULL; }
~Animal() { delete m_pRun, m_pEat; }
void Run() { if (m_pRun) m_pRun->run(); }
void Eat() { if (m_pEat) m_pEat->eat(); }
protected :
IRun * m_pRun;
IEat * m_pEat;
};
class Dog : public Animal
{
public:
Dog() { m_pRun = new DogRun; m_pEat = new DogEat;}
};
class Cat : public Animal
{
public:
Cat() { m_pRun = new CatRun; m_pEat = new CatEat;}
};
class Fish : public Animal
{
public;
Fish() { m_pEat = new FishEat; }
};
int main()
{
vector<Animal *> vctAnimals;
vctAnimals.push_back(new Dog());
vctAnimals.push_back(new Cat());
vctAnimals.push_back(new Fish());
for (int i=0; i<vctAnimals.size(); ++i)
{
vctAnimals[i].Run();
vctAnimals[i].Eat();
}
for (int i=0; i<vctAnimals.size(); ++i)
{
delete vctAnimals[i];
}
return 0;
}