c++ - 有没有办法专门化继承对象的一般方法

标签 c++ inheritance polymorphism

我有两个专门的对象:

class Food {};
class Fruit : public Food {};
class Vegetable : public Food {};

然后我有一个将被继承的父类:

class Parent
{
    virtual void say(Food* obj) { std::cout << "The object is food" << std::endl; }
};

还有一个从父级继承的类。

class Child : public Parent
{
    virtual void say(Fruit* obj) { std::cout << "The object is a fruit" << std::endl; }
    virtual void say(Vegetable* obj) { std::cout << "The object is a vegetable" << std::endl; }
};

我愿意:

std::vector<Food*> basket;
Fruit fruit = Fruit();
Vegetable vegetable = Vegetable();
basket.push_back(&fruit);
basket.push_back(&vegetable);

Child child = Child();

for (Food* food : basket)
{
    child.say(food);
}

我希望它打印“对象是水果”,然后是“对象是蔬菜”,但它不起作用: 我收到错误消息:参数 1 没有从“Food*”到“Fruit*”的已知转换。

有没有办法做到这一点,如果可能的话不使用 typeid,因为我听说它会导致开销。这是在线编辑器中的代码:cpp.sh/27ekc

最佳答案

我认为适当的解决方案如下:

class Food 
{
public:
    virtual ~Food() = default;
    virtual void say() const;
};
class Fruit : public Food 
{
public:
    void say() const override { std::cout << "The object is a fruit" << std::endl; }
};
class Vegetable : public Food 
{
public:
    void say() const override { std::cout << "The object is a vegetable" << std::endl; }
};

class Parent
{
public:
    virtual ~Parent() = default;
    virtual void say(const Food& obj) const { obj.say(); }
};

class Child : public Parent {};

int main()
{
    std::vector<Food*> basket;
    Fruit fruit = Fruit();
    Vegetable vegetable = Vegetable();
    basket.push_back(&fruit);
    basket.push_back(&vegetable);

    Child child = Child();

    for (const Food* food : basket)
    {
        child.say(*food);
    }
}

编辑: 根据您的评论,它基于您所说的健康。 我将其解释为以下几行:

class Food 
{
public:
    virtual ~Food() = default;
    virtual void say() const;
    virtual int health() const;
};
class Fruit : public Food 
{
public:
    void say() const override { std::cout << "The object is a fruit" << std::endl; }
    int health() const override { return 5; }
};
class Vegetable : public Food 
{
public:
    void say() const override { std::cout << "The object is a vegetable" << std::endl; }
    int health() const override { return 10; }
};

class Parent
{
public:
    virtual ~Parent() = default;
    virtual void say(const Food& obj) const { obj.say(); }
};

class Child : public Parent 
{
    int health;

public:
    void eat(const Food& obj) { health += obj.health(); }
};

int main()
{
    std::vector<Food*> basket;
    Fruit fruit = Fruit();
    Vegetable vegetable = Vegetable();
    basket.push_back(&fruit);
    basket.push_back(&vegetable);

    Child child = Child();

    for (const Food* food : basket)
    {
        child.say(*food);
        child.eat(*food);
    }
}

有很多不同的方法可以实现这一点。

关于c++ - 有没有办法专门化继承对象的一般方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41304907/

相关文章:

c++ - 从具有非虚拟父类的虚拟类继承的正确方法

java - 继承、方法签名、方法覆盖和抛出子句

c# - c++ auto_ptr 与托管指针有何关系(Java、C#...)

c++ - 关于 C++ 继承的初学者架构主题

c++ - 隐式字符转换

c# - 为什么要在 C# 中显式重写虚方法?

java - 在 java 映射中搜索对象

java - 使用注释@Loader 和@NamedQuery Hibernate 加载集合

c++ - Vista/Win7 低音和高音音量

c++ - 在32位数字中查找第一个(最低)置位位置