samedi 17 octobre 2020

Dynamic ad hoc polymorphism in C++

In my C++ code, I have a collection of classes A1, A2, ..., all derived from a class A. I also have a vector v<*A> holding pointers to objects all of type A. I want to implement a function foo(A *x, A *y) that is dynamically ad hoc polymorphic in the type of x and y. To make this more concrete, imagine A is Shape, A1, A2, ..., are Circle, Rect, ..., and foo is intersects(Shape *x, Shape *y).

I could overload foo with declarations like foo(A1 *x, A2 *y) for the combinations of derived types of A that I care about, however this will not work for the objects referenced by my vector v since function overloading, unlike virtual methods, is handled statically. I also cannot use virtual methods of the form A1::foo(A2 *y) since this only dynamically resolves the type of the method's class (A1) and not the type of the argument (A2).

The only solution I've thought of is to implement foo as follows:

void foo(A *x, A*y) {
  if (A1* a1 = dynamic_cast<A1*>(x)) {
    if (A1* a1 = dynamic_cast<A1*>(y)) {
      ...
    }
    ...
  }
  if (A2* a2 = dynamic_cast<A2*>(x)) {
    if (A1* a1 = dynamic_cast<A1*>(y)) {
      ...
    }  
    ...
  }
  ...
}

However, I've always been told that resorting to dynamic casts is rarely a good idea. Is there a more idiomatic way to achieve this?

Aucun commentaire:

Enregistrer un commentaire