如何在C++中声明抽象类的向量列表?

Ahm*_*mad 3 c++ polymorphism inheritance vector

我有几个类继承自一个主类。为了简单起见,我过度简化了类定义,使其简短明了。

动物.h

所有其他类都继承自的主类:

class Animal {
protected:
    string name;
public:
    Animal(string name);
    virtual string toString() { return "I am an animal"; }
};
Run Code Online (Sandbox Code Playgroud)

鸟.h

class Bird: public Animal {
private:
    bool canFly;
public:
    Bird(string name, bool canFly = true) 
     : Animal(name)   // call the super class constructor with its parameter
    {
        this->canFly = canFly;
    }
    string toString() { return "I am a bird"; }
};
Run Code Online (Sandbox Code Playgroud)

间接.h

class Insect: public Animal {
private:
    int numberOfLegs;
public:
    Insect(string name, int numberOfLegs) : Animal(name) {
        this->numberOfLegs = numberOfLegs;
    }
    string toString() { return "I am an insect."; }
};
Run Code Online (Sandbox Code Playgroud)

现在,我需要声明一个vector<Animal>将保存每个继承类的多个实例的对象。

主程序

#include <iostream>
#include "animal.h"
#include "bird.h"
#include "insect.h"

// assume that I handled the issue of preventing including a file more than once
// using #ifndef #define and #endif in each header file.

int main() {

    vector<Animal> creatures;

    creatures.push_back(Bird("duck", true));
    creatures.push_back(Bird("penguin", false));
    creatures.push_back(Insect("spider", 8));
    creatures.push_back(Insect("centipede",44));

    // now iterate through the creatures and call their toString()

    for(int i=0; i<creatures.size(); i++) {
        cout << creatures[i].toString() << endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

我期望以下输出:

我是一只鸟

我是一只鸟

我是一只昆虫

我是一只昆虫

但我得到了:

我是一只动物

我是一只动物

我是一只动物

我是一只动物

我知道这与“矢量生物”这句话有关。. It is calling the constructor for 动物生物动物. But my intention is to tell the compiler, that this 鸟类昆虫toString ()`。points to an array ofinherited classes, might bemight be, the point is: they all implement their own unique respective version of

我可以做什么来声明从同一祖先继承的对象的多态数组?

Pic*_*ent 5

您不能使用语义(阅读有关对象切片的内容)。您必须使用指针。

例子:

#include <iostream>
#include <memory>
#include <string>
#include <vector>

class Animal
{
 protected:
  std::string name;

 public:
  Animal(std::string name) : name(name)
  {
  }
  virtual std::string toString()
  {
    return "I am an animal";
  }
  virtual ~Animal()
  {
  }
};

class Bird : public Animal
{
 private:
  bool canFly;

 public:
  Bird(std::string name, bool canFly = true) : Animal(name)  // call the super class constructor with its parameter
  {
    this->canFly = canFly;
  }
  std::string toString()
  {
    return "I am a bird";
  }
};

class Insect : public Animal
{
 private:
  int numberOfLegs;

 public:
  Insect(std::string name, int numberOfLegs) : Animal(name)
  {
    this->numberOfLegs = numberOfLegs;
  }
  std::string toString()
  {
    return "I am an insect.";
  }
};

int main()
{
  std::vector<std::unique_ptr<Animal>> creatures;

  creatures.emplace_back(new Bird("duck", true));
  creatures.emplace_back(new Bird("penguin", false));
  creatures.emplace_back(new Insect("spider", 8));
  creatures.emplace_back(new Insect("centipede", 44));

  // now iterate through the creatures and call their toString()

  for (int i = 0; i < creatures.size(); i++)
  {
    std::cout << creatures[i]->toString() << std::endl;
  }
}
Run Code Online (Sandbox Code Playgroud)

印刷:

I am a bird
I am a bird
I am an insect.
I am an insect.
Run Code Online (Sandbox Code Playgroud)

我还建议阅读Sean Parent Run Time Polymorphism。想法如下:

#include <iostream>
#include <memory>
#include <string>
#include <vector>

class Animal
{
 public:
  struct Interface
  {
    virtual std::string toString() const = 0;
    virtual ~Interface()                 = default;
  };
  std::shared_ptr<const Interface> _p;

 public:
  Animal(Interface* p) : _p(p)
  {
  }
  std::string toString() const
  {
    return _p->toString();
  }
};

class Bird : public Animal::Interface
{
 private:
  std::string _name;
  bool        _canFly;

 public:
  Bird(std::string name, bool canFly = true) : _name(name), _canFly(canFly)
  {
  }
  std::string toString() const override
  {
    return "I am a bird";
  }
};

class Insect : public Animal::Interface
{
 private:
  std::string _name;
  int         _numberOfLegs;

 public:
  Insect(std::string name, int numberOfLegs)
      : _name(name), _numberOfLegs(numberOfLegs)
  {
  }
  std::string toString() const override
  {
    return "I am an insect.";
  }
};

int main()
{
  std::vector<Animal> creatures;

  creatures.emplace_back(new Bird("duck", true));
  creatures.emplace_back(new Bird("penguin", false));
  creatures.emplace_back(new Insect("spider", 8));
  creatures.emplace_back(new Insect("centipede", 44));

  // now iterate through the creatures and call their toString()

  for (int i = 0; i < creatures.size(); i++)
  {
    std::cout << creatures[i].toString() << std::endl;
  }
}
Run Code Online (Sandbox Code Playgroud)