Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Problem in private inheritance

Reply
Thread Tools

Problem in private inheritance

 
 
Bangalore
Guest
Posts: n/a
 
      10-03-2005
Hi,

I am not understanding the problem in the below program,
#include <iostrema.h>
class Base
{
public:
virtual void display()
{
cout <<"Base \n";
}
};
class Derivedrivate Base
{
public:
void display()
{
cout <<"Derived \n";
}
};
int man()
{
Base *ptr=new Derived;

ptr->display();
return 0;
}

If I compile it, if flashes error "cannot convert Derived * to
Base*"
Can anybody clarify , why we get this error, that is only with
private
inheritance.

Thanks,
Bangalore

 
Reply With Quote
 
 
 
 
FabioAng
Guest
Posts: n/a
 
      10-03-2005

"Bangalore" <(E-Mail Removed)-spam.invalid> wrote in
message news:4340df34$0$81711$(E-Mail Removed) eenews.net...
> Hi,
>
> I am not understanding the problem in the below program,
> #include <iostrema.h>
> class Base
> {
> public:
> virtual void display()
> {
> cout <<"Base \n";
> }
> };
> class Derivedrivate Base
> {
> public:
> void display()
> {
> cout <<"Derived \n";
> }
> };
> int man()
> {
> Base *ptr=new Derived;
>
> ptr->display();
> return 0;
> }
>
> If I compile it, if flashes error "cannot convert Derived * to
> Base*"
> Can anybody clarify , why we get this error, that is only with
> private
> inheritance.
>
> Thanks,
> Bangalore
>

Look at this:

http://www.parashift.com/c++-faq-lit...heritance.html

Bye
Fabio


 
Reply With Quote
 
 
 
 
Bangalore
Guest
Posts: n/a
 
      10-03-2005
Look at this:[/quote]

http://www.parashift.com/c++-faq-lit...heritance.html

Bye
Fabio[/quote]

Thanks Fabio.
In that FAQ , I could not understand some portion that is quoted.
Eventhough it has been clearly explained, but I am not understanding.
can anybody clarify this with small example.

24.4] Should I pointer-cast from a private derived class to its base
class?

Generally, No.

> "--->From a member function or friend of a privately derived

class, the relationship to the base class is known, and the upward
conversion from PrivatelyDer* to Base* (or PrivatelyDer& to
Base&) is safe; no cast is needed or recommended"..

However users of PrivatelyDer should avoid this unsafe conversion,
since it is based on a private decision of PrivatelyDer, and is
subject to change without notice.

 
Reply With Quote
 
Zorro
Guest
Posts: n/a
 
      10-03-2005
I read [24.4], and I will rephrase it. That does not mean I agree with
it.

Inside the definition of bodies of methods of class PrivatelyDer (and
friends of it), you can do as you have done in main(), without casting.
However, other functions such as main() are users of this class. Such
users need explicit cast, like (Base*). It also says to avoid doing
that, but I do not quite see the point in the warning stated there.

I hope this helps. I know it is irritating. The general idea however
lies in the private derivation which makes the Base private to the
Derived class, and therefore not accessible. I would appreciate if
someone more knowledgeable points to an item in the standard that
explains this behavior, in the context of polymorphism.

Regards,
http://www.velocityreviews.com/forums/(E-Mail Removed)
http://www.zhmicro.com
http://distributed-software.blogspot.com
http://groups-beta.google.com/group/...Z?lnk=la&hl=en

 
Reply With Quote
 
Bangalore
Guest
Posts: n/a
 
      10-04-2005
Thanks a lot,
Bangalore

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Private Inheritance and Publice Inheritance karthikbalaguru C++ 9 09-10-2007 01:05 PM
The problem of inheritance? Why can't it overload the private same name function ?Help? dragon C++ 5 12-22-2005 02:40 AM
Private/protected inheritance problem Mark A. Gibbs C++ 3 03-12-2005 05:59 PM
Should 'public virtual' always become 'private virtual'? & using private inheritance qazmlp C++ 19 02-04-2004 12:37 AM
Private access modifier and Inheritance (Inheritance implementation in Java) maxw_cc Java 1 12-21-2003 11:38 AM



Advertisments