mercredi 23 décembre 2015

Nested classes encapsulation

I want to encapsulate several methods in a nested class (t1) and call them from the nesting (derived) class and from the base abstract class (base), and also to have access to some data members of the nesting (derived) class from the nested class (t1), but I can't get it to work.

Is it possible to do it? This is what I tried:

class base {
public:
    virtual void baz() = 0;
};

class derived : public base {
    int a1, a2;
    virtual void baz() {
        tt.foo(); // member is inaccessible 
        int v1 = 1;
    }

    class t1 {
        int foo() { return 2; }
    };
public:

private:
    t1 & tt;
};

int main(){
    base *base;
    (?)
    base->baz();
    base->tt.foo();
}

Aucun commentaire:

Enregistrer un commentaire