为什么派生*到基类*之间的转换与私有继承失败? [英] Why would the conversion between derived* to base* fails with private inheritance?
问题描述
这是我的代码 -
#include< iostream>
using namespace std;
class base
{
public:
void sid()
{
}
};
class derived:private base
{
public:
void sid()
{
}
}
int main()
{
base * ptr;
ptr = new derived; //错误:'base'是'derived'的不可访问的基础
ptr-> sid();
return 0;
}
这会产生编译时错误。
错误:'base'是'derived'的不可访问的基础
pre>
由于编译器会尝试调用基类
sid()
为什么会出现这个错误?有人可以解释一下。解决方案$ 11.2 / 4州 -
< >
如果
- ,那么B的一个发明的公共成员将可以访问R的基类B N的公共成员或
- R发生在类N的成员或朋友中,并且发明的公众
的B成员将是私人的或
的保护N的成员或 - R发生在从N派生的类P的成员或朋友中,并且
发明的B的公共成员将是
私人或P的受保护成员,或 - ,存在一个类S,使得B是可在R
访问的S的基类,S是N访问的基类
at R。
这里'B'是'Base','N' '派生'和'R'是主要的。
-
考虑第二个项目符号 - 这个子句不适用于'R'(main)既不是'N'(Derived)的成员也不是朋友。
-
考虑第三个项目符号 - 'R发生在P类的成员或朋友....。
-
考虑第四个项目符号。此条款不适用
因此,我们可以得出结论,'Base'不是一个可访问的'Derived'类。
$ 11.2 / 5 states -
如果一个基类是
可访问,可以隐式转换
a指针一个派生类到该基类的一个
指针(4.10,
4.11)。 [注意:因此,类X的成员和朋友可以
隐式地将X *转换为指针
到私有或受保护的立即
基类X. -end note]
由于 Base
不是可访问类
在 main
中访问时,从Derived类到Base类的标准转换是错误的。因此错误。
编辑2:
研究一些流行的编译器的错误消息,得到更好的理解。请注意不可访问一词在所有错误消息中如此频繁和一致地弹出。
引用来自草案标准N3000。我还没有下载最新的草稿:)
GCC prog.cpp:在函数'int
main()': prog.cpp:27:错误:'base'是
'derived'的不可访问的基础
Comeau在线ComeauTest.c,第26行:
错误:转换为不可访问的基础
类base不是
允许
ptr =新派生;
VS2010错误C2243:'type cast':
从派生*到基本
*的转换存在,但不可访问
Here is my code -
#include<iostream>
using namespace std;
class base
{
public:
void sid()
{
}
};
class derived : private base
{
public:
void sid()
{
}
};
int main()
{
base * ptr;
ptr = new derived; // error: 'base' is an inaccessible base of 'derived'
ptr->sid();
return 0;
}
This gives a compile time error.
error: 'base' is an inaccessible base of 'derived'
Since the compiler will try and call the base class sid()
why do I get this error? Can someone please explain this.
$11.2/4 states-
A base class B of N is accessible at R, if
- an invented public member of B would be a public member of N, or
- R occurs in a member or friend of class N, and an invented public member of B would be a private or protected member of N, or
- R occurs in a member or friend of a class P derived from N, and an invented public member of B would be a private or protected member of P, or
- there exists a class S such that B is a base class of S accessible at R and S is a base class of N accessible at R."
Here 'B' is 'Base', 'N' is 'Derived' and 'R' is main.
Consider the 2nd bullet- 'R occurs in a member or friend of a class N,...'. This clause does not apply as 'R'(main) is neither a member nor friend of 'N'(Derived)
Consider the 3rd bullet- 'R occurs in a member or friend of a class P....'. This claus also does not apply for the same reasons as above
Consider the 4th bullet- Once again this clause does not apply
Thus we can conclude that 'Base' is not an accessible class of 'Derived'.
$11.2/5 states -
If a base class is accessible, one can implicitly convert a pointer to a derived class to a pointer to that base class (4.10, 4.11). [ Note: it follows that members and friends of a class X can implicitly convert an X* to a pointer to a private or protected immediate base class of X. —end note ]
Since Base
is not an accessible class of Derived
when accessed in main
, the Standard conversion from Derived class to Base class is ill-formed. Hence the error.
EDIT 2:
Study the error messages of some popular compilers and that should help you get a better understanding. Note how the word 'inaccessible' pops up so frequently and consistently across all the error messages
The references are from the draft standard N3000. I am yet to download the latest draft :)
GCC prog.cpp: In function ‘int main()’: prog.cpp:27: error: ‘base’ is an inaccessible base of ‘derived’
Comeau Online "ComeauTest.c", line 26: error: conversion to inaccessible base class "base" is not allowed ptr = new derived;
VS2010 error C2243: 'type cast' : conversion from 'derived *' to 'base *' exists, but is inaccessible
这篇关于为什么派生*到基类*之间的转换与私有继承失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!