Member Funktionen Überladen bei Vererbungen



  • Hallo,
    hab grade ein kleines Problem trotz mehrmaligen lesen verstehe ich folgenden Text nicht:

    C++ Primer 5th schrieb:

    The access to the overloaded
    versions that are not otherwise redefined by the derived class will be the access in
    effect at the point of the using declaration.

    Hier ein größterer Ausschnitt damit ihr auch wisst worum es geht.

    C++ Primer 5th schrieb:

    Instead of overriding every base-class version that it inherits, a derived class can
    provide a using declaration (§15.5, p. 615) for the overloaded member. A using
    declaration specifies only a name; it may not specify a parameter list. Thus, a using
    declaration for a base-class member function adds all the overloaded instances of that
    function to the scope of the derived class. Having brought all the names into its scope,
    the derived class needs to define only those functions that truly depend on its type. It
    can use the inherited definitions for the others.
    The normal rules for a using declaration inside a class apply to names of
    overloaded functions (§15.5, p. 615); every overloaded instance of the function in the
    base class must be accessible to the derived class. The access to the overloaded
    versions that are not otherwise redefined by the derived class will be the access in
    effect at the point of the using declaration.

    Hoffe mir kann das jemand erklären.



  • Das bedeutet nur, dass die Sichtbarket ( private, protected, public ) der deklarierten Member davon abhängt, wie die Sichtbarkeit an der Stelle ist, wo entweder die using Deklaration steht, oder eine Überladung definiert wird.

    z.B.

    struct base{
      void nufnuf() const {}
      void nifnif() const {}
    };
    
    class derived : public base{
      using base::nufnuf; // private!
    public:
      using base::nifnif;
    };
    
    int main(){
      derived d;
      d.nifnif();
      d.nufnuf(); // Fehler (inaccessible)
    }
    

  • Mod

    Es geht um Sichtbarkeit bei using. Zum Beispiel:

    class base
    {
    protected:
      void foo();
    };
    
    class derived : public base
    {
    public:
      using base::foo;  // foo ist nun public
    };
    

    Das hat dann auch Auswirkungen auf die Sichtbarkeit und das Überschreiben von Funktionen durch die abgeleitete Klasse. Für Beispiele dazu, siehe:
    http://en.cppreference.com/w/cpp/language/using_declaration



  • Danke euch beiden. Ist ja irgendwie einleuchtend.


Anmelden zum Antworten