Topic: protected inheritance problem - compiler bug ?
Author: Niklas Mellin <nimel@my-deja.com>
Date: 2000/10/13 Raw View
In article <39E58D13.358BC7FD@tre.ntc.nokia.com>,
ext-pratik.khasnabis@nokia.com wrote:
[...]
> Please comment about the behaviour of this code.
> =======================CODE================================
> #include <iostream>
>
> using namespace std;
> class Base
> {
> public:
> Base():a(0){}
> int a;
> };
>
> class PubD : public Base
> {
> };
>
> class PrivD : private Base
> {
> };
>
> class ProtD : protected Base
> {
> };
>
> class PubProtD : public ProtD
> {
> void myfunc(PubD *pD1 , PrivD *pD2 , ProtD *pD3) {
>
> Base *pB;
>
> pB = pD1;
> pB->a = 2;
>
> //pB = pD2;
> //pD2->a = 2;
> pB = pD3; //--------------------- >> error in this line ???
> //pD3->a = 2;
>
> }
> };
>
> int main() {
> return 0;
> }
Gcc and Borland are correct (even though gcc's error message is
misleading), Comeau, HP aCC, and you are wrong. Protected inheritance
means that the derived classes can access public and protected members
of the base class *in its own object*.
/Niklas Mellin
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: wmm@fastdial.net
Date: 2000/10/13 Raw View
In article <8s7als$ouf$1@nnrp1.deja.com>,
Niklas Mellin <nimel@my-deja.com> wrote:
> In article <39E58D13.358BC7FD@tre.ntc.nokia.com>,
> ext-pratik.khasnabis@nokia.com wrote:
>
> [...]
> > Please comment about the behaviour of this code.
>
> > =======================CODE================================
> > #include <iostream>
> >
> > using namespace std;
> > class Base
> > {
> > public:
> > Base():a(0){}
> > int a;
> > };
> >
> > class PubD : public Base
> > {
> > };
> >
> > class PrivD : private Base
> > {
> > };
> >
> > class ProtD : protected Base
> > {
> > };
> >
> > class PubProtD : public ProtD
> > {
> > void myfunc(PubD *pD1 , PrivD *pD2 , ProtD *pD3) {
> >
> > Base *pB;
> >
> > pB = pD1;
> > pB->a = 2;
> >
> > //pB = pD2;
> > //pD2->a = 2;
> > pB = pD3; //--------------------- >> error in this line ???
> > //pD3->a = 2;
> >
> > }
> > };
> >
> > int main() {
> > return 0;
> > }
>
> Gcc and Borland are correct (even though gcc's error message is
> misleading), Comeau, HP aCC, and you are wrong. Protected inheritance
> means that the derived classes can access public and protected members
> of the base class *in its own object*.
Well, that's not quite the rule (see 11.5: it has to be via a
pointer to its own class or one derived from it, but it could
be in any object, not just *this). However, I think the
conclusion is right. The applicable description is in 11.2p4.
The question of whether PubProtD::my_func can convert pD3 to
Base* is equivalent to whether it could access an invented
public member of Base via pD3. The answer is "no" -- a public
member of Base is a protected member in ProtD, and ProtD is
not PubProtD nor a class derived from it. Thus the invented
member is inaccessible and the conversion is not permitted.
--
William M. Miller, wmm@fastdial.net
Vignette Corporation (www.vignette.com)
Sent via Deja.com http://www.deja.com/
Before you buy.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: pratik khasnabis <pratik.khasnabis@tre.ntc.nokia.com>
Date: 2000/10/12 Raw View
Hi
I was following the example in Bjarne's book (C.11.2). I am including
the minimal compilable version of my code. I tried four different
compilers. Stroustrup means something like this. Because class Base is a
protected base of ProtD , members and friends of ProtD's derived
classes (eg PubProtD) can (impicitly) convert a ProtD* to Base* , just
as they can access public and protected members of class Base.
If there is a bug in the Borland and GNU compiler it is serious indeed
as concept of protected is fundamental to C++.In fact gcc's error
message says ProtD has a private inheritance which is totally
incomprehensible. In fact I was thinking some kind of protected
inheritance for a curious design problem. Now this result got me
thinking.
Please comment about the behaviour of this code.
Pratik
=======================CODE================================
#include <iostream>
using namespace std;
class Base
{
public:
Base():a(0){}
int a;
};
class PubD : public Base
{
};
class PrivD : private Base
{
};
class ProtD : protected Base
{
};
class PubProtD : public ProtD
{
void myfunc(PubD *pD1 , PrivD *pD2 , ProtD *pD3) {
Base *pB;
pB = pD1;
pB->a = 2;
//pB = pD2;
//pD2->a = 2;
pB = pD3; //--------------------- >> error in this line ???
//pD3->a = 2;
}
};
int main() {
return 0;
}
===============================RESULTS=======================
Comeau Compiler 4.2.44 (on the web):-
Compile succeeded
HP aCC 3.13 :-
OK
gcc 2.95.2:-
In method `void PubProtD::myfunc(PubD *, PrivD *, ProtD *)':
accesstobase.cc: fields of `Base' are inaccessible in `ProtD' due to
private inheritance
Borland5.5 bcc32:-
accesstobase.cpp : Cannot convert 'ProtD *' to 'Base *' in function
PubProtD::myfunc(PubD *,PrivD *,ProtD *)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]
Author: llewelly.@@edevnull.dot.com
Date: 2000/10/13 Raw View
pratik khasnabis <pratik.khasnabis@tre.ntc.nokia.com> writes:
[snip]
> gcc 2.95.2:-
> In method `void PubProtD::myfunc(PubD *, PrivD *, ProtD *)':
> accesstobase.cc: fields of `Base' are inaccessible in `ProtD' due to
> private inheritance
I believe gcc's behavior in this example is a bug. You can report it
by following the instructions at gcc.gnu.org/bugs.html
>
> Borland5.5 bcc32:-
> accesstobase.cpp : Cannot convert 'ProtD *' to 'Base *' in function
> PubProtD::myfunc(PubD *,PrivD *,ProtD *)
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.research.att.com/~austern/csc/faq.html ]
[ Note that the FAQ URL has changed! Please update your bookmarks. ]