Problem in private inheritance

B

Bangalore

Hi,

I am not understanding the problem in the below program,
#include <iostrema.h>
class Base
{
public:
virtual void display()
{
cout <<"Base \n";
}
};
class Derived:private 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
 
F

FabioAng

Bangalore said:
Hi,

I am not understanding the problem in the below program,
#include <iostrema.h>
class Base
{
public:
virtual void display()
{
cout <<"Base \n";
}
};
class Derived:private 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-lite/private-inheritance.html

Bye
Fabio
 
B

Bangalore

Look at this:[/quote]

http://www.parashift.com/c++-faq-lite/private-inheritance.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.
 
Z

Zorro

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,
(e-mail address removed)
http://www.zhmicro.com
http://distributed-software.blogspot.com
http://groups-beta.google.com/group/computerlangZ?lnk=la&hl=en
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top