What's the point of IsA() in C++?

前端 未结 5 1264
鱼传尺愫
鱼传尺愫 2021-02-10 16:14

I\'m trying to figure out why some code bases use IsA() to determine object polymorphism if in C++ you can already safely upcast and down cast (using dynamic_cast) ?

So

5条回答
  •  南旧
    南旧 (楼主)
    2021-02-10 17:17

    There are few reasons where and IsA() function, or even a dynamic_cast<>() are needed in C++. The worst examples of this type of code are the giant if-then statements using dynamic_casts, or switch statements on a type field. These represent a maintenance nightmare, where adding a class can involve updating dozens, or hundreds of different locations to support the new class.

    For example:

    Bad:

    // Don't do this:
    void PrintName(Base *b, ostream &o)
    {
       if (dynamic_cast(b) != NULL)
          o << "Derived A";
       if (dynamic_cast(b) != NULL)
          o << "Derived B";
       if (dynamic_cast(b) != NULL)
          o << "Derived C";
    }
    

    Better:

    void PrintName(Base *b, ostream &o)
    {
       o << b->GetName();
    }
    

    This is obviously eliding checking for null, and using smart pointers, etc.. Likewise, if you're querying the type to choose between different behaviours, you need to ask why you're doing something different for each type, and move that behaviour decision into the object.

    Bad:

    // Don't do this:
    void ObjectBase::ApplyForceToObject(const Force &f)
    {
        if (dynamic_cast(this) != NULL
            || dynamic_cast(b) != NULL)
        {
            // Do nothing
        }
        else
        {
            // Accelerate object
        }
    }
    

    Better:

    void ObjectBase::ApplyForceToObject(const Force &f)
    {
        if (IsFixedObject())
        {
            // Do nothing
        }
        else
        {
            // Accelerate object
        }
    }
    ...
    bool ObjectBase::IsFixedObject() { return false; }
    bool Wall::IsFixedObject() { return true; }
    bool Floor::IsFixedObject() { return true; }
    

提交回复
热议问题