1

我得到的错误obj.a::get();是a类无法访问,我知道这obj.get()会标记一个错误,但我想这应该可以,这里有什么问题?

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();
}
4

1 回答 1

10

c用于private派生自a,因此不能从不是 的成员函数的函数访问的a子对象。cc

于 2013-04-30T07:47:18.873 回答