C++ C++;基类指针、集合类

C++ C++;基类指针、集合类,c++,inheritance,polymorphism,C++,Inheritance,Polymorphism,我有一个基类动物和派生类狗,猫 我还有一个DogCollection、CatCollection类来管理操作,例如添加新的cat等,读取cat,从数据库中删除cat,使用指向Dog和cat类的指针搜索特定的cat 我被要求使用基类指针来管理单个容器中的类。在狗和猫类中执行读写操作不是更好的做法,而不是单独的DoCub和CcDeCon类用于此目的?< P>普通C++,通常使用模板容器来保存对象,例如: #include <vector> class Cat; class Dog; c

我有一个基类动物和派生类狗,猫

我还有一个DogCollection、CatCollection类来管理操作,例如添加新的cat等,读取cat,从数据库中删除cat,使用指向Dog和cat类的指针搜索特定的cat


我被要求使用基类指针来管理单个容器中的类。在狗和猫类中执行读写操作不是更好的做法,而不是单独的DoCub和CcDeCon类用于此目的?

< P>普通C++,通常使用模板容器来保存对象,例如:

#include <vector>

class Cat;
class Dog;
class Animal;

typedef std::vector<Cat*> CatCollection;
typedef std::vector<Dog*> DogCollection;
typedef std::vector<Animal*> AnimalCollection;
#包括
猫类;
班犬;
类动物;
typedef std::向量CatCollection;
typedef std::向量集合;
typedef std::vector作为容器,但还有其他可用的容器

然后将容器作为容器进行操作,并对项目本身执行操作,如:

AnimalCollection coll;

//add elements
Cat *cat = ...;
Dog *dog = ...;

coll.push_back(cat);
coll.push_back(dog);

//do something with the first item of the collection
coll[0] -> doStuff();

//do something on all items
for (Animal *c: coll) {
    c -> doStuff();
}

//Don't forget to delete allocated objects one way or the other
//std::vector<std::unique_ptr<Animal>> can for example take ownership of pointers and delete them when the collection is destroyed
animalcoll;
//添加元素
猫*猫=。。。;
狗*狗=。。。;
coll.推回(cat);
coll.推回(狗);
//对收藏的第一项进行处理
coll[0]->doStuff();
//对所有项目都做些什么
用于(动物*c:coll){
c->doStuff();
}
//别忘了以这种或那种方式删除分配的对象

//STD::Cudio

普通C++,通常使用模板容器来保存对象,例如:

#include <vector>

class Cat;
class Dog;
class Animal;

typedef std::vector<Cat*> CatCollection;
typedef std::vector<Dog*> DogCollection;
typedef std::vector<Animal*> AnimalCollection;
#包括
猫类;
班犬;
类动物;
typedef std::向量CatCollection;
typedef std::向量集合;
typedef std::vector作为容器,但还有其他可用的容器

然后将容器作为容器进行操作,并对项目本身执行操作,如:

AnimalCollection coll;

//add elements
Cat *cat = ...;
Dog *dog = ...;

coll.push_back(cat);
coll.push_back(dog);

//do something with the first item of the collection
coll[0] -> doStuff();

//do something on all items
for (Animal *c: coll) {
    c -> doStuff();
}

//Don't forget to delete allocated objects one way or the other
//std::vector<std::unique_ptr<Animal>> can for example take ownership of pointers and delete them when the collection is destroyed
animalcoll;
//添加元素
猫*猫=。。。;
狗*狗=。。。;
coll.推回(cat);
coll.推回(狗);
//对收藏的第一项进行处理
coll[0]->doStuff();
//对所有项目都做些什么
用于(动物*c:coll){
c->doStuff();
}
//别忘了以这种或那种方式删除分配的对象

//请出示一些代码。你的最后一段让我有点困惑。我知道这是家庭作业。听起来你被要求使用
动物集合
而不是
狗集合
猫集合
,但我无法理解你的最后一个问题。据我所知,你被要求拥有一个指向
动物
的指针容器,在那里你可以存储指向
的指针。所以,我想,你说的是虚拟调度。我相信你要找的是
狗wMyDog
Animal*wAnimal=wMyDog请显示一些代码。你的最后一段让我有点困惑。我知道这是家庭作业。听起来你被要求使用
动物集合
而不是
狗集合
猫集合
,但我无法理解你的最后一个问题。据我所知,你被要求拥有一个指向
动物
的指针容器,在那里你可以存储指向
的指针。所以,我想,你说的是虚拟调度。我相信你要找的是
狗wMyDog
Animal*wAnimal=wMyDog
这取决于您可能需要对实例执行特定操作,或者您希望封装在不同类中的某些数组操作。@coyotte508 doStuff()属于哪个类?如果它是
虚拟的
,并且在子类
Cat
Dog
中被重写,然后将调用
Cat
Dog
的一个。我将很快使用一个实例进行编辑。这取决于您可能需要对实例执行特定操作,或者需要将某些数组操作封装到一个不同的类中。@coyotte508 doStuff()对哪个类执行操作属于?如果它是
virtual
并且在子类
Cat
Dog
中被重写,则将调用
Cat
Dog
的一个。我将很快用一个实例进行编辑。