从derived *到base *的转换存在但无法访问 [英] conversion from derived * to base * exists but is inaccessible
本文介绍了从derived *到base *的转换存在但无法访问的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
为什么以下代码会产生此错误,即使c是结构并且默认情况下具有公共继承?
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 = ⅆ
cc->fun();
}
推荐答案
您需要:
class d : public c
class
默认情况下继承 private
。
当您私下从类
或 struct
继承时,您明确说明了直接转换从派生类型到基类型是不可能的。
When you privately inherit from a class
or a struct
, you explicitly say, among others, that direct conversion from a derived type to a base type isn't possible.
这篇关于从derived *到base *的转换存在但无法访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文