不需要执行运行
这个问题在这里已经有了答案:
class Base {
public:
virtual ~Base() {}
// ...
};
class Derived : public Base {
// ...
};
“典型用途”:
void foo(Derived*);
void f(Base* pb)
{
if (Derived* pd = dynamic_cast<Derived*>(pb)) {
foo(pd);
}
}
“以上报价”:
void bar(Base*);
void f(Derived* pd)
{
Base* pb = dynamic_cast<Base*>(pd); // the dynamic_cast is useless here
// because a Derived IS-A Base, always
bar(pb); // Note: could as well call directly bar(pd); (implicit conversion)
}
dynamic_cast
主要用于向下和交叉播放。 这个问题提到了天性。
结构B1,B2,D:B1,B2:
上一篇: cast not needing to perform a run
下一篇: C++ Casting Operators and traditional C casting operators