Ken Li
Ken Li

Reputation: 2618

Function that takes a base class pointer in C++ that is overloaded with a pointer to a subclass

This is the example code I have:

#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;
}

The output of this code can be found here

http://ideone.com/I29g3A

and it outputs:

Caller calls animal
Caller calls animal

I'm wondering, without casting a specific element to Rabbit*, is there a way to get call(Rabbit *r) method to get called?

Upvotes: 0

Views: 82

Answers (1)

Dietmar K&#252;hl
Dietmar K&#252;hl

Reputation: 153840

Sure, e.g., by jumping through a suitable visitor in your system of polymorphic classes. I think you'll need to use two names instead of call(), however. I used pubCall() and 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;
}

Upvotes: 3

Related Questions