Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

conversion from derived * to base * exists but is inaccessible

Why does the follwing code produce this error even though c is a struct and has a public inheritance by default??

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();
}
like image 248
user1232138 Avatar asked May 06 '12 18:05

user1232138


1 Answers

You need:

class d : public c

class inheritance is private by default.

When you privately inherit from a class or a struct, you explicitly say, among other things, that direct conversion from a derived type to a base type isn't possible.

like image 192
Luchian Grigore Avatar answered Oct 21 '22 17:10

Luchian Grigore