我想做这样的事情,假设Lion
并且Seal
是该类的子类Animal
:
class Zoo
{
public:
Zoo(std::vector<Lion> lions1, std::vector<Seal> seals1) : lions(lions1), seals(seals1)
{
unsigned int i;
for (i=0; i<lions.size(); ++i)
{
animals.push_back(&(lions[i]));
}
for (i=0; i<seals.size(); ++i)
{
animals.push_back(&(seals[i]));
}
}
private:
std::vector<Animal*> animals;
std::vector<Lion> lions;
std::vector<Seal> seals;
};
这似乎不起作用。在我看来,当我这样做时它不起作用,animals.push_back(&(lions[i]));
或者animals.push_back(&(seals[i]));
,指针出现问题。知道为什么吗?
提前致谢!