如何使用多态从基类访问派生类向量成员? [英] How to use polymorphism to access derived class vector member from base class?

查看:165
本文介绍了如何使用多态从基类访问派生类向量成员?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据说,通过多态,我们可以使用它的基类对象访问派生类成员字段,如下所示:

It's said that with polymorphism, we can access a derived class member field with it's base class object like this:

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

using namespace std;

class Tool{
    public:
        Tool(){}
        Tool(string name){
            this->name = name;
        }
        virtual string getInfo(){
            return name;
        }
    //protected:
        string name;
};

class Computer: public Tool{
    public:
        Computer(string name, int price){
            this->name = name;
            this->price = price;
        }
        virtual string getInfo(){
            return name + ": " + to_string(static_cast<long long>(price));
        }
    //protected:
        int price;
};

class Person{
    public:
        Person(){}
        Person(string name){
            this->name = name;
        }
        virtual string getInfo(){
            return name;
        }
        virtual void addTools(Computer cmp){
            tools.push_back(cmp);
        }
    //protected:
        vector<Tool> tools;
        string name;
};

class Programmer: public Person{
    public:
        Programmer(string name, string job){
            this->name = name;
            this->job = job;
        }
        string getInfo(){
            return name + ": " + job;
        }
    //protected:
        string job;
};

int main(){
    Person prs("Person");
    Programmer prg("Daphloon", "programmer");

    Person* prs1 = &prs;
    Person* prs2 = &prg;

    cout << prs1->getInfo() << endl;    // result: Person
    cout << prs2->getInfo() << endl;    // result: Daphoon: programmer

    Tool tl("Hammer");
    Computer cmp("PC", 100);

    Tool* tl1 = &tl;
    Tool* tl2 = &cmp;


    cout << tl1->getInfo() << endl;     // result: Hammer
    cout << tl2->getInfo() << endl;     // result: PC: 100

    prs2->addTools(cmp);

    cout << prs2->tools[0].getInfo() << endl;   // result: PC
                                                // I expect the result will be: PC: 100 

    return 0;
}

结果不是我的预期。我需要的是来自 Person 的每个派生类都有一个向量 tools ,它包含从工具类。如果用word描述,它将是This Person Programmer ,有一些 tools 。他的第一个工具是一个计算机。如果你想知道它是什么的价格,使用 getInfo()

The result wasn't what I expected. What I need is every derived class from Person have a vector tools that contains object that inherit from Tool class. If it described with word, it will be, "This Person, a Programmer, has some tools. His first Tool is a Computer. If you want to know what's it's price, use getInfo()."


  1. 为什么vector取基类而不是派生类?

  2. cmp 对象是否有任何数据丢失当我把它放在工具 vector?

  3. 这是否发生是因为 tools 矢量成员拿工具因为它的类型?

  1. Why vector take the base class instead of the derived class?
  2. Is there any data loss from cmp object when i put it inside tools vector?
  3. Is this happen because tools vector member take Tool as it's type?


推荐答案

C ++中的运行时多态性,通过虚函数实现,适用于 covariant 类型。唯一的协变类型是指针和引用。由于你有一个向量<工具> ,你会失去多态性。要保留它,请存储向量<工具*> 。更好的是,存储向量< unique_ptr<工具>>

Runtime polymorphism in C++, achieved via virtual functions, works on covariant types. The only covariant types are pointers and references. Since you have a vector<Tool>, you lose polymorphism. To retain it, store a vector<Tool*>. Even better, store a vector<unique_ptr<Tool>>.

将派生类对象分配给基础class被称为对象切片。您确实会丢失派生对象中包含的信息。当您将计算机插入向量<工具> 时,就是这种情况。

Assigning a derived class object to a base class is called object slicing. You do lose information contained in the derived object. This is the case when you are inserting a Computer into a vector<Tool>.

这篇关于如何使用多态从基类访问派生类向量成员?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆