С помощью наследования вы можете вызвать функцию родительского указателя и выполнить дочернюю функцию. Но если есть два родительских указателя, я не знаю, как сделать так, чтобы каждый тип указателя имел значение.
Ниже приведен пример, очень близкий к тому, что я пытаюсь заставить работать:
Код: Выделить всё
class Shape {
bool intersects(const Circle& other) const = 0;
bool intersects(const Line& other) const = 0;
}
class Circle : public Shape {
bool intersects(const Circle& other) const;
bool intersects(const Line& other) const;
}
class Line : public Shape {
bool intersects(const Circle& other) const;
bool intersects(const Line& other) const;
}
bool intersect(Shape* s1, Shape* s2) {
// this does not compile.
return s1->intersects(*s2);
}
Код: Выделить всё
class Shape {
}
class Circle : public Shape {
}
class Line : public Shape {
}
bool intersect(const Circle& s1, const Circle& s2) const;
bool intersect(const Circle& s1, const Line& s2) const;
bool intersect(const Line& s1, const Circle& s2) const;
bool intersect(const Line& s1, const Line& s2) const;
bool intersect(Shape* s1, Shape* s2) {
// this does not compile.
return intersect(*s1, *s2);
}
Если возможно, я бы не хотел вручную проверять typeid во время выполнения или динамический_cast, пока не получу ненулевой указатель.>
Подробнее здесь: https://stackoverflow.com/questions/798 ... ion-that-t
Мобильная версия