J
jsendrow
FireFlow hat gesagt.:Das was du mit dem Satz meinst (der ist komisch formuliert) stimmt nicht. Wenn du eine Methode überschreibst *MUSS* sie den gleichen Rückgabetyp haben. Du kannst jediglich eine neue Methode mit selben bezeichner deklarieren wenn sie sich in Anzahl und/oder Typen der Übergabeparameter, oder in der "Constness" unterscheidet. Aber dann hat das mit Überschreiben nichts mehr zu tun, das ist dann überladen und die Funktion in der Kindklasse überdeckt die der Basisklasse.
Gruß
Stimmt, ungünstig formuliert, aber schau mal hier:
Covariant Returns. A long-standing C++ rule requires that a member function that overrides a virtual function must not only have the same signature but also the same return value as the base class member function. In the following code, for example, A::f and B::f both return a pointer to an object of class X.
class X
{};
class A
{
public:
virtual X* f() {return new X;}
};
class B : public A
{
public:
virtual X* f() {return new X;}
};
In real-world object models, however, it is not unusual for B::f to want to return a pointer to an object derived from X (such parallel hierarchies are really quite common). ISO C++ allows such covariant returns, so you can modify the code as follows:
class Y : public X
{};
class B : public A
{
public:
virtual Y* f() {return new Y;}
};
Zuletzt bearbeitet von einem Moderator: