看到下面的代碼:將這項工作類型轉換C++
ap = &abr;
爲什麼這裏的代碼使用動態轉換從here
採取
void simple_dynamic_casts()
{
AB ab;
B* bp = (B*)&ab; // cast needed to break protection
A* ap = &ab; // public derivation, no cast needed
AB& abr = dynamic_cast<AB&>(*bp); // succeeds
ap = dynamic_cast<A*>(bp); assert(ap != NULL);
bp = dynamic_cast<B*>(ap); assert(bp == NULL);
ap = dynamic_cast<A*>(&abr); assert(ap != NULL);
bp = dynamic_cast<B*>(&abr); assert(bp == NULL);
}
代碼。我還沒有試過編譯這段代碼。
爲什麼-1表示 – 2009-11-07 18:44:19