为什么不能访问类A函数在下面的代码? [英] why cant i access class A function in following code?
问题描述
我得到 obj.a :: get();
的错误是类a是不可访问的,我知道 obj.get
会标记一个错误,但我想这应该工作,这里的问题是什么?
{
int arg1;
public:
a(int i){
arg1 = i;
cout<< a()调用<< endl
}
void get(){
cout< arg1 =<< arg1<< endl
}
};
class b {
int arg2;
public:
b(int j){
arg2 = j;
cout<< b()调用<< 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()调用<< endl
}
};
int main(){
c obj(1,2,3);
obj.a :: get();
}
the error i get for
这篇关于为什么不能访问类A函数在下面的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! c 使用
private
派生自 a
,因此 a
c
的成员函数的函数无法访问的
子对象
p> 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();
}
c
uses private
to derive from a
, so the a
subobject of c
is inaccessible from functions which are not member functions of c
.