Überladung in Basisklasse bereitstellen



  • Wie kann ich eine Überladung einer Memberfunktion in einer Basis Klasse hinzufügen?

    Also das ich bei folgenden Code als ausgabe 01 und nicht 00 erhalte:

    #include <iostream>
    
    struct b {
        int k(double) {
            return 1;
        }
    };
    
    struct a : public b {
        int k(int) {
            return 0;
        }
    };
    
    int main() {
        a a;
        std::cout << a.k(int()) << a.k(double());
    }
    

    mfg.



  • Ich hab's durch rumprobieren gerade selber gelöst:

    #include <iostream>
    
    struct b {
        int k(double) {
            return 1;
        }
    };
    
    struct a : public b {
        using b::k;
        int k(int) {
            return 0;
        }
    };
    
    int main() {
        a a;
        std::cout << a.k(int()) << a.k(double());
    }
    

    mfg.



  • wieder so Ding, wo ich das Verhalten des Programmes nicht nachvollziehen kann...

    Sollte sich k(double) nicht eigentlich schon (eben durch die Vererbung) in der Stuktur befinden und k(int) nur eine Erweiterung sein?



  • MaxDemian schrieb:

    wieder so Ding, wo ich das Verhalten des Programmes nicht nachvollziehen kann...

    Sollte sich k(double) nicht eigentlich schon (eben durch die Vererbung) in der Stuktur befinden und k(int) nur eine Erweiterung sein?

    Ja, das versteh ich auch nicht.

    mfg.



  • ISO-Standard schrieb:

    When two or more different declarations are specified for a single name in the same scope, that name is said to be overloaded. By extension, two declarations in the same scope that declare the same name but with different types are called overloaded declarations. Only function declarations can be overloaded; object and type declarations cannot be overloaded.

    ISO-Standard schrieb:

    13.2 Declaration matching [over.dcl]
    A function member of a derived class is not in the same scope as a function member of the same name in a base class.

    Überladene Funktionen müssen also im selben Scope sein, bei einer abgeleiteten Klasse sind sie das aber nicht.

    Hoffe, ich konnte helfen

    Felix



  • Phoemuex schrieb:

    ISO-Standard schrieb:

    When two or more different declarations are specified for a single name in the same scope, that name is said to be overloaded. By extension, two declarations in the same scope that declare the same name but with different types are called overloaded declarations. Only function declarations can be overloaded; object and type declarations cannot be overloaded.

    ISO-Standard schrieb:

    13.2 Declaration matching [over.dcl]
    A function member of a derived class is not in the same scope as a function member of the same name in a base class.

    Überladene Funktionen müssen also im selben Scope sein, bei einer abgeleiteten Klasse sind sie das aber nicht.

    Hoffe, ich konnte helfen

    Felix

    Ja, danke. Also ist das mit dem using der richtige Weg oder?

    Wie kann ich den gesamtem Scope der Basisklasse in den Scope der abgeleiteten packen? Geht das?

    mfg.




Anmelden zum Antworten