存在从派生*到基本*的转换,但无法访问


88

为什么即使c是一个结构并且默认情况下具有公共继承,以下代码也会产生此错误?

struct c 
{
protected:
    int i;
public:
    c(int ii=0):i(ii){}
    virtual c *fun();
};

c* c::fun(){
    cout<<"in c";
    return &c();
}

class d : c
{
 public:
    d(){}
    d* fun()
    {
        i = 9;
        cout<<"in d"<<'\t'<<i;
        return &d();
    }
};


int main()
{
    c *cc;
    d dd;
    cc = &dd;
    cc->fun();
}

Answers:


167

你需要:

class d : public c

class继承private默认情况下。

当您私有地从class或继承时struct,您明确地说,从派生类型到基本类型的直接转换是不可能的。


格里戈尔先生再次赢得胜利。谢谢你,先生!您的回答确实有帮助!
M.Ionut
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.