在C ++中使用指向子类的指针重载的基类指针的函数

时间:2013-11-15 21:55:25

标签: c++ inheritance

这是我的示例代码:

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

class Animal {

};

class Rabbit : public Animal {

};

class Caller {
    public:
    virtual void call(Animal* a) {
        std::cout << "Caller calls animal" << std::endl;
    }

    virtual void call(Rabbit* r) {
        std::cout << "Caller calls rabbit" << std::endl;
    }
};


int main(int argc, char** argv) {
    std::vector<Animal*> v;
    Caller c;
    auto a = new Animal();
    auto r = new Rabbit();
    v.push_back(a);
    v.push_back(r);


    for(auto elem : v) {
        c.call(elem);
    }


    return 0;
}

此代码的输出可在此处找到

http://ideone.com/I29g3A

并输出:

Caller calls animal
Caller calls animal

我想知道,如果没有向Rabbit*投射特定元素,是否有办法让call(Rabbit *r)方法被调用?

1 个答案:

答案 0 :(得分:3)

当然,例如,通过在您的多态类系统中跳过合适的访问者。不过,我认为您需要使用两个名称而不是call()。我使用了pubCall()call()

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

class Visitor;

class Animal {
public:
    virtual void visit(Visitor&);
};

class Rabbit : public Animal {
    void visit(Visitor&);
};

class Visitor
{
public:
    virtual void call(Animal* a) = 0;
    virtual void call(Rabbit* r) = 0;
};

void Animal::visit(Visitor& v) {
    v.call(this);
}

void Rabbit::visit(Visitor& v) {
    v.call(this);
}

class Caller
    : Visitor {
public:
    void pubCall(Animal* a) { a->visit(*this); }

private:
    virtual void call(Animal* a) {
        std::cout << "Caller calls animal" << std::endl;
    }

    virtual void call(Rabbit* r) {
        std::cout << "Caller calls rabbit" << std::endl;
    }
};


int main(int argc, char** argv) {
    std::vector<Animal*> v;
    Caller c;
    auto a = new Animal();
    auto r = new Rabbit();
    v.push_back(a);
    v.push_back(r);


    for(auto elem : v) {
        c.pubCall(elem);
    }


    return 0;
}