1

the error i get for obj.a::get(); is class a is inaccessible, i know that obj.get()would flag an error, but i guess this should work, whats the issue here?

class a {
    int arg1;
public:
    a(int i){
        arg1 = i;
        cout << "a() called" << endl;
    }

    void get() {
        cout << "arg1=" << arg1 << endl;
    }
};


class b {
    int arg2;
public:
    b(int j) {
        arg2 = j;
        cout << "b() called" << endl;
    }

    void get() {
        cout << "arg2=" << arg2 << endl;
    }
};

class c: private a, private b {
    int arg3;
public:
    c(int i, int j, int k): b(k), a(j) {
        arg3 = k;
        cout << "c() called" << endl;
    }
};

int main() {
    c obj(1, 2, 3);
    obj.a::get();
}
Peter Wood
  • 23,859
  • 5
  • 60
  • 99
Praveen Kumar
  • 55
  • 1
  • 8

1 Answers1

10

c uses private to derive from a, so the a subobject of c is inaccessible from functions which are not member functions of c.

Andy Prowl
  • 124,023
  • 23
  • 387
  • 451
  • This `c obj(1,2,3); obj.a::get();` makes me think, that's not the only problem. – Kiril Kirov Apr 30 '13 at 07:48
  • @KirilKirov: Perhaps I am missing something obvious, but... why so? – Andy Prowl Apr 30 '13 at 07:51
  • `class a` is base class (even thought `private`) of `class c`. Having the code in the question, even if `a` was a `public` derived, can you access base class' function like this: `obj.a::get(); /*where obj is object c*/`? Or I'm missing something here? – Kiril Kirov Apr 30 '13 at 08:10
  • `get()` is defined in both `classes a and b` so when he inherits them in `c` there would be ambiguity. `::` should resolve it – Koushik Shetty Apr 30 '13 at 08:12
  • @KirilKirov: I would say you can. See, for instance, [this](http://ideone.com/U77jDM). – Andy Prowl Apr 30 '13 at 08:15
  • Wow, I wasn't familiarized with this kind of syntax o.O You're right, my bad. // +1 – Kiril Kirov Apr 30 '13 at 08:19