-1

For example i have three classes

class Shape
{
  static bool collide(Box * a, Box * b)
  static bool collide(Box * a, Sphere * b)
  static bool collide(Sphere * a, Sphere * b)
};

class Box : public Shape{};

class Sphere : public Shape{};

and for example somewhere else i have this piece of code

Shape * a = new Box();
Shape * b = new Sphere();

Shape::collide(a, b);

The compiler raises and error.

How can i automatically downcast objects given as argument without adding too much logic?

1 Answers1

0

The compiler can't cast the objects automatically, you will have to cast them manually, eg:

class Box;
class Sphere;

class Shape
{
  virtual ~Shape(){}

  static bool collide(Shape *a, Shape *b);
  static bool collide(Box *a, Box *b);
  static bool collide(Box *a, Sphere *b);
  static bool collide(Sphere *a, Sphere *b);
};

class Box : public Shape{};

class Sphere : public Shape{};

bool Shape::collide(Shape *a, Shape *b)
{
  if (Box *a_box = dynamic_cast<Box*>(a))
  {
    if (Box *b_box = dynamic_cast<Box*>(b))
      return collide(a_box, b_box);

    if (Sphere *b_sphere = dynamic_cast<Sphere*>(b))
      return collide(a_box, b_sphere);
  }
  else if (Sphere *a_sphere = dynamic_cast<Sphere*>(a))
  {
    if (Sphere *b_sphere = dynamic_cast<Sphere*>(b))
      return collide(a_sphere, b_sphere);
  }
  return false;
}

bool Shape::collide(Box *a, Box *b) { ... }
bool Shape::collide(Box *a, Sphere *b) { ... }
bool Shape::collide(Sphere *a, Sphere *b) { ... }
Shape * a = new Box();
Shape * b = new Sphere();

Shape::collide(a, b);

Needless to say, that can get a bit tedious as new shapes are added. You should instead add a single virtual method to Shape itself and let the derived classes override it to handle different types as needed, eg:

class Shape
{
  virtual ~Shape(){}
  virtual bool collide(Shape * b) { return false; };
};

class Box : public Shape{
  bool collide(Shape * b) override {
    if (Box *b_box = dynamic_cast<Box*>(b)) {
      return ...;
    }
    if (Sphere *b_sphere = dynamic_cast<Sphere*>(b)) {
      return ...;
    }
    return Shape::collide(b);
  };
};

class Sphere : public Shape{
  bool collide(Shape * b) override { 
    if (Sphere *b_sphere = dynamic_cast<Sphere*>(b)) {
      return ...;
    }
    return Shape::collide(b);
  };
};
Shape * a = new Box();
Shape * b = new Sphere();

a->collide(b);
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770