为什么不能访问类A函数在下面的代码? [英] why cant i access class A function in following code?

查看:101
本文介绍了为什么不能访问类A函数在下面的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到 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();
}


解决方案

c 使用 private 派生自 a ,因此 a c 的成员函数的函数无法访问子对象 p>

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

解决方案

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

这篇关于为什么不能访问类A函数在下面的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆