Skip to content

Latest commit

 

History

History
41 lines (33 loc) · 821 Bytes

c26466.md

File metadata and controls

41 lines (33 loc) · 821 Bytes
title ms.date f1_keywords helpviewer_keywords description
Warning C26466
03/22/2018
C26466
NO_STATIC_DOWNCAST_POLYMORPHIC
C26466
CppCoreCheck rule that enforces C++ Core Guidelines Type.2

Warning C26466

Don't use static_cast downcasts. A cast from a polymorphic type should use dynamic_cast.

See also

C++ Core Guidelines Type.2

Example

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*
    }
}