警告 C26466

请勿使用 static_cast 向下转换。 从多态类型强制转换应使用 dynamic_cast。

另请参阅

C++ Core Guidelines Type.2

示例

struct Base {
    virtual ~Base();
};

struct Derived : Base {};

void bad(Base* pb)
{
    Derived* test = static_cast<Derived*>(pb); // C26466
}

void good(Base* pb)
{
    if (Derived* pd = dynamic_cast<Derived*>(pb))
    {
        // ... do something with Derived*
    }
    else
    {
        // ... do something with Base*
    }
}