Mit der Vererbung können Sie die Funktion eines übergeordneten Zeigers aufrufen und die untergeordnete Funktion ausführen. Aber wenn es zwei übergeordnete Zeiger gibt, weiß ich nicht, wie ich jeden Zeigertyp „materiell“ machen soll.
Unten ist ein Beispiel, das dem, was ich zum Laufen bringen möchte, sehr nahe kommt:
Code: Select all
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);
}
Code: Select all
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);
}
Wenn möglich, möchte ich die manuelle Überprüfung von typeid zur Laufzeit oder von „dynamic_cast“ vermeiden, bis ich einen Nicht-Null-Zeiger erhalte.
Mobile version