简体   繁体   English

C++ 将子类的对象添加到父 class 的向量中

[英]C++ Adding objects of child classes to a vector of a parent class

I have the following class hierarchy: Crocodile Class extends from Oviparous which extends from Animal我有以下 class 层次结构:鳄鱼 Class 从卵生延伸,从动物延伸

I need to store objects of type Crocodile, Goose, Pelican, Bat, Whale and SeaLion inside a vector, so:我需要将 Crocodile、Goose、Pelican、Bat、Whale 和 SeaLion 类型的对象存储在向量中,因此:

1- I create the global vector: 1-我创建全局向量:

vector<Animal*> animals;

2- I add objects (Crocodile, Goose, Pelican, Bat, Whale, SeaLion) to the vector: 2- 我将对象(鳄鱼、鹅、鹈鹕、蝙蝠、鲸鱼、海狮)添加到向量中:

animals.push_back(new Crocodile(name, code, numberofEggs));

3- I loop through the vector to print each object on a table 3-我循环遍历向量以在桌子上打印每个 object

for (size_t i = 0; i < animals.size(); ++i){
    /* now the problem is here, each animal[i] is of type = "Animal", not Crocodile, or Goose, etc..
   /* so when I try to do something like the line below it doesn't work because it can't find the method because that method is not on the Animal Class of course */
   cout << animals[i]->GetName(); // THIS WORK
   cout << animals[i]->GetNumberofEggs(); //THIS DOESN'T WORK
   /* when I debug using the line below, every object on the vector is returning "P6Animal" */
   cout << typeid(animals[i]).name(); // P6Animal instead of Crocodile
}

I think it is related with this post std::vector for parent and child class and I think the problem is object slicing, so I tried creating the vector like this:我认为它与父子 class 的这篇帖子 std::vector 有关,我认为问题是 object 切片,所以我尝试像这样创建向量:

vector<unique_ptr<Animal>> animals;
//and adding the objects like this
animals.push_back(unique_ptr<Animal>(new Crocodile(name, code, numberofEggs)));

But nothing但是什么都没有

Any help would be great!任何帮助都会很棒! Thank you!谢谢!

Your problem is that you try to access to specific child's method, that not defined in the parent class, from an object that has a parent type.您的问题是您尝试从具有父类型的 object 访问未在父 class 中定义的特定子方法。

You should store an object as a type you want to work with , so if you store an array of Animal implies that you will work with objects using only the Animal interface, regardless of the initial object type.您应该将 object 存储为要使用的类型,因此,如果您存储Animal数组,则意味着您将仅使用Animal接口处理对象,而不管初始 object 类型如何。 Otherwise, if you need to access methods from Opivarous class you should think about store your objects as Opivarous (or Crocodile , etc).否则,如果您需要从Opivarous class 访问方法,您应该考虑将您的对象存储为Opivarous (或Crocodile等)。

Also, you can use dynamic_cast for this purpose:此外,您可以为此目的使用dynamic_cast

if(Crocodile* crocodile = dynamic_cast<Crocodile*>(animals[i]))
{
    cout << crocodile->GetNumberofEggs();
}

It is the simplest way to solve a problem, but it shows that there are problems with architecture in your code.这是解决问题的最简单方法,但它表明您的代码中存在架构问题。

I got it working with having a virtual "Print" method on the Animal class, then override this method from the child classes.我让它在动物 class 上使用虚拟“打印”方法,然后从子类中覆盖此方法。 Thank you everyone!谢谢大家!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM