Topic: Private inheritance is useless
Author: "Philippe A. Bouchard" <philippeb@corel.com>
Date: Mon, 19 Mar 2001 19:32:26 GMT Raw View
Is there any reason why private and protected inheritance exists? You
just have to cast the object to its base class to bypass the scope
check. OK, its not working with dynamic_cast<>(), but with
static_cast<>(), it is:
#include <iostream>
struct A
{
void foo() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
};
struct B : private A
{
};
int main()
{
B b;
static_cast<A &>(b).foo();
}
--
Philippe A. Bouchard
Corel Linux
---
[ 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 ]
Author: Francis Glassborow <francis.glassborow@ntlworld.com>
Date: Tue, 20 Mar 2001 00:40:28 GMT Raw View
In article <3AB65955.5954C321@corel.com>, Philippe A. Bouchard
<philippeb@corel.com> writes
>Is there any reason why private and protected inheritance exists? You
>just have to cast the object to its base class to bypass the scope
>check. OK, its not working with dynamic_cast<>(), but with
>static_cast<>(), it is:
>
>#include <iostream>
>
>struct A
>{
> void foo() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
>};
>
>struct B : private A
>{
>};
>
>int main()
>{
> B b;
> static_cast<A &>(b).foo();
I think that line should generate a diagnostic.
>}
--
Francis Glassborow
See http://www.accu.org for details of The ACCU Spring Conference, 2001
(includes many regular participants to C & C++ newsgroups)
---
[ 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 ]
Author: "James Kuyper Jr." <kuyper@wizard.net>
Date: Tue, 20 Mar 2001 03:15:48 GMT Raw View
"Philippe A. Bouchard" wrote:
>
> Is there any reason why private and protected inheritance exists? You
> just have to cast the object to its base class to bypass the scope
> check. OK, its not working with dynamic_cast<>(), but with
> static_cast<>(), it is:
The private/protected inheritance serves to prevent implicit use of base
class. Being able to bypass that protection doesn't render it pointless.
You can tilt a glass to pour the water out; that doesn't make the sides
of the glass pointless.
In any event, I don't believe that's a legal conversion. I can't find
Derived* -> InaccessibleBase* in the list of permitted conversions for
static_cast<>. 4.10p3 allows Derived* -> Base*, but it's ill-formed if
Base is inaccessible. 5.2.9p2 permits the static_cast<> (through 8.5p14)
only if that conversion is well-formed.
---
[ 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 ]
Author: James Dennett <jdennett@acm.org>
Date: Tue, 20 Mar 2001 03:35:13 GMT Raw View
Francis Glassborow wrote:
>
> In article <3AB65955.5954C321@corel.com>, Philippe A. Bouchard
> <philippeb@corel.com> writes
> >Is there any reason why private and protected inheritance exists? You
> >just have to cast the object to its base class to bypass the scope
> >check. OK, its not working with dynamic_cast<>(), but with
> >static_cast<>(), it is:
> >
> >#include <iostream>
> >
> >struct A
> >{
> > void foo() { std::cout << __PRETTY_FUNCTION__ << std::endl; }
> >};
> >
> >struct B : private A
> >{
> >};
> >
> >int main()
> >{
> > B b;
> > static_cast<A &>(b).foo();
>
> I think that line should generate a diagnostic.
I agree, as does Comeau's online compiler. The only way to force
access to a private base is with a C-style cast.
As another data point, gcc 3.0 (prerelease) still appears to have
a bug here, and compiles the code without a diagnostic.
-- James Dennett
---
[ 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 ]