这是我的示例代码:
#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;
}
此代码的输出可在此处找到
并输出:
Caller calls animal
Caller calls animal
我想知道,如果没有向Rabbit*
投射特定元素,是否有办法让call(Rabbit *r)
方法被调用?
答案 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;
}