Conversion from derived * to base * exists but not available

Why does the error code generate this error, although c is a structure and has public default inheritance?

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(); } 
+72
c ++ inheritance
May 6 '12 at 18:00
source share
1 answer

You need:

 class d : public c 

By default, class inheritance is private .

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

+143
May 6 '12 at 18:02
source share
— -



All Articles