Re: how to use private inheritance
On Aug 22, 7:36 pm, Markus Moll <markus.m...@esat.kuleuven.ac.be>
wrote:
Hi
zhangyefei.ye...@gmail.com wrote:
#include <iostream>
using namespace std;
class a
{
public:
virtual void doit() {cout<<"a\n";};
};
class b: private a
{
public:
void doit() {cout<<"b\n";}
};
class c
{
public:
void set(a * pa) { m_a =pa;m_a->doit();};
a * m_a;
};
int main ()
{
c cc;
cc.set(new b);
return 0;
}
but when i change source code sightly ,still with private
inheritance, everything is ok,this surprise me.
[...]
class b: a
{
public:
void doit() {cout<<"b\n";}
void go() { c cc;cc.set(this);};
};
[...]
the above two program seem same to me,but the results arte complete
different.
why ? can anyone do me a favor of giving any hints ?
The question is who is converting the pointer to b to a pointer to a.
In the first program, the function main is trying to do so, in the second=
,
it's a member function of b. Just like private members can only be access=
ed
by members of the same class, private base-classes can only be accessed b=
y
members of the class. This means that any member of b is allowed to conve=
rt
a pointer to b to a pointer to a.
Markus
it seems to be best answer resolving questions about this usage of
private inheritance .
my last question is , who is converting the pointer to b to a pointer
to a ? i thinks it is compiler doing this ,is not it ?
thanks.