conversion from derived * to base * exists but is inaccessible

30,183

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.

Share:
30,183
user1232138
Author by

user1232138

Updated on July 08, 2022

Comments

  • user1232138
    user1232138 almost 2 years

    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();
    }
    
  • M.Ionut
    M.Ionut about 4 years
    Mr Grigore once again for the win. Thank you, kind sir! Your answers really help!