任何人都可以在之前和之后显示示例代码以避免向访问者模式代码进行向下转换吗?
感谢。
答案 0 :(得分:21)
一个简单的,简约的例子。
class Base {};
class Derived1 : public Base {};
class Derived2 : public Base {};
// Some arbitrary function that handles Base.
void
Handle(Base& obj) {
if (...type is Derived1...) {
Derived1& d1 = static_cast<Derived1&>(base);
std::printf("Handling Derived1\n");
}
else if (...type is Derived2...) {
Derived2& d2 = static_cast<Derived2&>(base);
std::printf("Handling Derived2\n");
}
}
这意味着Base
必须有一些类型标记字段,或者您将使用dynamic_cast
来检查每种类型。
// Class definitions
class Visitor;
class Base {
public:
// This is for dispatching on Base's concrete type.
virtual void Accept(Visitor& v) = 0;
};
class Derived1 : public Base {
public:
// Any derived class that wants to participate in double dispatch
// with visitor needs to override this function.
virtual void Accept(Visitor& v);
};
class Derived2 : public Base {
public:
virtual void Accept(Visitor& v);
};
class Visitor {
public:
// These are for dispatching on visitor's type.
virtual void Visit(Derived1& d1) = 0;
virtual void Visit(Derived2& d2) = 0;
};
// Implementation.
void
Derived1::Accept(Visitor& v) {
v.Visit(*this); // Calls Derived1 overload on visitor
}
void
Derived2::Accept(Visitor& v) {
v.Visit(*this); // Calls Derived2 overload on visitor
}
那是框架。现在,您实现了实际的访问者以多态方式处理对象。
// Implementing custom visitor
class Printer : public Visitor {
virtual void Visit(Derived1& d1) { std::printf("Handling Derived1\n"); }
virtual void Visit(Derived2& d2) { std::printf("Handling Derived2\n"); }
};
// Some arbitrary function that handles Base.
void
Handle(Base& obj)
{
Printer p;
obj.Accept(p);
}
Accept()
是一个虚拟函数,可以调度obj
(第一次调度)的类型Visit()
的适当重载,因为在Accept()
内你已经知道对象的类型。Visit()
是一个虚拟函数,可以调度访问者的类型(第二次调度)。因为您有双重调度(一个在对象上,另一个在访问者上),所以您不进行任何投射。缺点是,无论何时向层次结构中添加类,都必须更新访问者类以添加适当的函数来处理新的子类。
答案 1 :(得分:4)
wikipedia示例使用双重调度,没有向下转发。