protcted Elemente



  • template<class T,template<class>class bar>
    class foo
    {
    protected:
        T x0,x1,x2,x3;
    };
    
    template<class T,template<class>class bar>
    class foo_abgl
        :public foo<T, bar>
    {
    public:
        T var1(){return x0;};
    };
    

    Was ist hier falsch? Ich bekomme undeclared identifer?



  • Hi, also ne Begründung kann ich dir nicht wirklich geben.
    Nur ist es so das du angeben musst das du auf einen Parentmember zugreifen willst

    template<class T,template<class>class bar>
    class foo
    {
    protected:
        T x0,x1,x2,x3; // Parent member
    };
    
    template<class T,template<class>class bar>
    class foo_abgl
        :public foo<T, bar>
    {
        typedef foo<T,bar> parent_type;
    public:
        T var1(){return parent_type::x0;};
    };
    

    So lässt sich das kompilieren.

    MfG

    PS: Ich wüsste auch gerne woran es genau liegt.

    Ich vermute das es daran liegt das man genau spezifizieren muss auf was für eine Basis man zugreift, also auf welche Spezialisierung von Foo. Aber das ist halt ne Spekulation. 🙄



  • Ja, das geht. Ich benutze zum ersten mal protected Elemente und in meinem Buch stand das so wie ichs gemacht hab. (Man musss aber sagen, dass templates später kamen, vielleicht hab ich dort eine Anmerkung überlesen...)



  • Es wird noch besser:

    foo_abgl<T,bar>& operator+=(const foo_abgl<T,bar>& lhs)
    {
        foo<T,bar>::x0=lhs.x0;
        return *this;
    };
    

    Das entzieht sich meiner Logik...



  • ness schrieb:

    Es wird noch besser:

    foo_abgl<T,bar>& operator+=(const foo_abgl<T,bar>& lhs)
    {
        foo<T,bar>::x0=lhs.x0;
        return *this;
    };
    

    Das entzieht sich meiner Logik...

    hmm, guck mal das geht auch:

    template<class T,template<class>class bar>
    class foo
    {
    protected:
        T x0,x1,x2,x3; // Parent member
    };
    
    template<class T,template<class>class bar>
    class foo_abgl
        :public foo<T, bar>
    {
        typedef foo<T,bar> parent_type;
    public:
        T var1(){return this->x0;}
    
        foo_abgl<T,bar>& operator+=(const foo_abgl<T,bar>& lhs){
           this->x0=lhs.x0;
           return *this;
        }
    };
    

    Daher sollte es sich nicht deiner Logik entziehen.

    MfG

    Edit: Achja, und da sich via this der genaue Typ ermitteln lässt, ist das wohl dann nicht notwendig das man das spezifiziert.



  • typedef foo_abgl<T,bar> my_type;
    

    wuerde das ganze auch noch leserlicher machen 😉



  • Das hat hier nichts mit protected zu tun. Das gilt auch für public Elemente. Das ganze nennt sich Two Stage Name Lookup wird ganz gut hier beschrieben:

    http://gcc.gnu.org/onlinedocs/gcc-3.4.3/gcc/Name-lookup.html#Name-lookup



  • @ Ponto:
    Danke für die Info. 👍


Anmelden zum Antworten